C# LINQ:如何检索单个结果
对 linq 有点陌生,
使用 linq 检索单个结果的最简单方法是什么?
例如,我的查询
var query =
from c in db.productInfo
where c.flavor == "Classic Coke" && c.container == "Can"
select c.co2Target;
应该只返回具有双精度值的单个字段。我如何将其从查询中取出?过去我使用过 ExecuteScalar。我如何使用 linq 做到这一点?我想保留其数据类型
更新:
这就是我现在所在的位置。问题是我在这里运行的测试查询返回 4 而不是 3.75
var query =
(from a in db.LUT_ProductInfos
where a.flavor == "Classic Coke" && a.Container == "Can"
select new { a.co2High }).Single();
double MyVar = query.co2High.Value;
Kind of new to linq,
whats the simplest way to retrieve a single result using linq?
example, my query
var query =
from c in db.productInfo
where c.flavor == "Classic Coke" && c.container == "Can"
select c.co2Target;
it should only return a single field with a double value. how do i pull it out of query? In the past i had used ExecuteScalar. How do i do it with linq? I would like to preserve its data type
UPDATE:
Here's where I am now. The problem is that the test query im running here is returning 4 instead of 3.75
var query =
(from a in db.LUT_ProductInfos
where a.flavor == "Classic Coke" && a.Container == "Can"
select new { a.co2High }).Single();
double MyVar = query.co2High.Value;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为你的意思是返回一个值,而不是一条记录?您需要按如下方式
select new {}
:然后,如果您只想检索单个记录:则
检索将按如下方式完成:
I think you mean return one value, not one record? You would need to do
select new {}
as follows:Then if you only want to retrieve a single record as well as that:
Retrieval would be done as follows:
使用
.Single()
或.SingleOrDefault()
扩展方法。Use the
.Single()
or.SingleOrDefault()
extension methods.通过使用
First()
或FirstOrDefault()
仅使用
Single()
或SingleOrDefault()
(如果您知道的话)只有一个结果,或者如果有多个结果你想失败。By using
First()
orFirstOrDefault()
Only use
Single()
orSingleOrDefault()
if you know there is only one result, or if you want to fail if there are multiple results.您可以使用
Single
扩展方法:其他相关的扩展方法是
SingleOrDefault
,第一个
和FirstOrDefault
。Single 和 First 之间的区别在于,如果查询结果有多个结果,则 Single 会引发异常。如果查询没有返回结果,OrDefault 变体将返回
null
,而 Single 和 First 则在不存在结果时抛出异常。如果您使用的是 Entity Framework 3.5,它不支持
Single
,因此您必须使用First
。另一件值得注意的事情是,您的原始代码生成了
IQueryable
,这意味着在您评估结果之前它不会实际执行查询。使用任何这些扩展方法都会强制查询立即运行。You can use the
Single
extension method:Other related extension methods are
SingleOrDefault
,First
andFirstOrDefault
.The difference between Single and First is that Single throws an exception if the query results in more than one result. The OrDefault variations will return
null
if no results were returned by the query, while Single and First throw an exception is no result exists.If you're using Entity Framework 3.5, it does not support
Single
, so you will have to useFirst
.One other thing worth noting is that your original code resulted in an
IQueryable<T>
, which means it does not actually execute the query until you evaluate the result. Using any of these extension methods will force the query to run immediately.msdn : SingleOrDefault
使用
Single()
或SingleOrDefault()
方法获取结果另请检查:默认扩展方法
msdn : SingleOrDefault
Make use of
Single()
orSingleOrDefault()
method to get resultAlso check : Default Extension methods
如果您的查询始终只返回一个元素作为结果,请使用
SingleOrDefault()
,否则如果您的查询结果超过一个元素,则会抛出异常。如果您的结果有多个元素并且您需要其中任何一个,请使用 FirstOrDefualt() 。
use
SingleOrDefault()
if your query always returns only one element as result or exception will be thrown if the result of your query is more than one element.use
FirstOrDefualt()
if your result more than one element and you need any one of then.我更喜欢 SingleOrDefault(),如果没有返回任何内容,它不会抛出异常。 MSDN 参考
这样您就可以对此类进行安全保护条件检查案件。
i prefer SingleOrDefault(), it does not throw an exception if the nothing is returned. MSDN reference
this way you can do a safe-guard condition check for such case.