C# 如何将 IQueryable<string> 转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11326468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I convert IQueryable<string> to string?
提问by thechmodmaster
I do a sql query which returns a string - service name. this is the query:
我做了一个 sql 查询,它返回一个字符串 - 服务名称。这是查询:
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?
如何从查询中获取字符串?
采纳答案by Anders Abel
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.
LINQ 总是返回一个序列,因此您必须从中检索项目。如果您知道您将只有一个结果,请使用Single()检索该项目。
var item = (from Comp in ServiceGroupdb.ServiceGroupes
where (Comp.GroupID == groupID)
select Comp.Name).Single();
There are four LINQ methods to retrieve a single item out of a sequence:
有四种 LINQ 方法可以从序列中检索单个项目:
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 (nullforstring). 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)
Single()返回项目,如果序列中有 0 个或多个项目,则抛出异常。SingleOrDefault()返回项目或默认值(nullforstring)。如果序列中有多个项目,则抛出。First()返回第一项。如果序列中有 0 个项目,则抛出。FirstOrDefault()返回第一项,如果没有项则返回默认值)
回答by Filip Ekberg
To get the first element in your query, you can use query.First()but if there are no elements, that would throw an exception. Instead, you can use query.FirstOrDefault()which will give you either the first string, or the default value (null). So for your query this would work:
要获取查询中的第一个元素,您可以使用query.First()但如果没有元素,则会引发异常。相反,您可以使用query.FirstOrDefault()which 将为您提供第一个字符串或默认值 ( null)。因此,对于您的查询,这将起作用:
var myString = (from Comp in ServiceGroupdb.ServiceGroupes
where Comp.GroupID == groupID
select Comp.Name)
.FirstOrDefault();
回答by Davio
You're almost there.
您快到了。
Just do
做就是了
IQueryable<string> query = from Comp in ServiceGroupdb.ServiceGroupes where (Comp.GroupID == groupID) select Comp.Name;
// Loop over all the returned strings
foreach(var s in query)
{
Console.WriteLine(s);
}
Or use query.FirstOrDefault()as mentioned as you'll only get one result.
或者query.FirstOrDefault()像上面提到的那样使用,因为你只会得到一个结果。
回答by Alex
I find the methods'way is prettier and clearer, so here it goes:
我发现方法的方式更漂亮更清晰,所以这里是:
string query = ServiceGroupdb.ServiceGroupes
.Where(Comp => Comp.GroupID == groupID)
.Select(Comp => Comp.Name)
.FirstOrDefault();
回答by Ebad Masood
Just do it like this;
就这样做;
var query = from Comp in ServiceGroupdb.ServiceGroupes where (Comp.GroupID == groupID) select Comp.Name;
query will then contain your result.
然后查询将包含您的结果。

