我怎么可以转换的IQueryable&LT;字符串&GT;串? [英] how can I convert IQueryable<string> to string?
本文介绍了我怎么可以转换的IQueryable&LT;字符串&GT;串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做一个SQL查询,它返回一个字符串 - 服务名。这是查询:
I do a sql query which returns a string - service name. this is the query:
IQueryable<string> query = from Comp in ServiceGroupdb.ServiceGroupes
where (Comp.GroupID == groupID)
select Comp.Name;
我如何得到串出查询?
How do i get the string out of the query?
推荐答案
LINQ总是返回一个序列,所以你要检索的项目出来。如果你知道你将只有一个结果,使用单()
来检索项目。
LINQ always returns a sequence, so you have to retrieve the item out of it. If you know that you will have only one result, use Single()
to retrieve that item.
var item = (from Comp in ServiceGroupdb.ServiceGroupes
where (Comp.GroupID == groupID)
select Comp.Name).Single();
有四种LINQ方法来检索单个项目出了顺序:
There are four LINQ methods to retrieve a single item out of a sequence:
-
单()
返回的项目,抛出一个异常,如果有0或序列中的多个项目。 -
的SingleOrDefault()
返回的项目,或默认值(空
为字符串
)。如果在序列中一个以上的项目。 引发
-
一()
返回的第一个项目。抛出,如果有0项的顺序进行。 -
FirstOrDefault()
返回的第一个项目,或默认值,如果没有项目)
Single()
returns the item, throws an exception if there are 0 or more than one item in the sequence.SingleOrDefault()
returns the item, or default value (null
forstring
). Throws if more than one item in the sequence.First()
returns the first item. Throws if there are 0 items in the sequence.FirstOrDefault()
returns the first item, or the default value if there are no items)
这篇关于我怎么可以转换的IQueryable&LT;字符串&GT;串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文