C# LINQ:如何检索单个结果

发布于 2024-11-07 14:59:35 字数 645 浏览 0 评论 0原文

对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

烟若柳尘 2024-11-14 14:59:35

我认为你的意思是返回一个值,而不是一条记录?您需要按如下方式 select new {}

var query =
     from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select new { c.co2Target };

然后,如果您只想检索单个记录:则

var query =
     (from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select new { c.co2Target }).Single();

检索将按如下方式完成:

var query =
         (from c in db.productInfo
         where c.flavor == "Classic Coke" && c.container == "Can"
         select new { c.co2Target }).Single();

double MyVar = query.co2Target;

I think you mean return one value, not one record? You would need to do select new {} as follows:

var query =
     from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select new { c.co2Target };

Then if you only want to retrieve a single record as well as that:

var query =
     (from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select new { c.co2Target }).Single();

Retrieval would be done as follows:

var query =
         (from c in db.productInfo
         where c.flavor == "Classic Coke" && c.container == "Can"
         select new { c.co2Target }).Single();

double MyVar = query.co2Target;
傲影 2024-11-14 14:59:35

使用 .Single().SingleOrDefault() 扩展方法。

var query =
     (from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select c.co2Target).Single();

Use the .Single() or .SingleOrDefault() extension methods.

var query =
     (from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select c.co2Target).Single();
ゃ人海孤独症 2024-11-14 14:59:35

通过使用 First()FirstOrDefault()

var query =
    (from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select c.co2Target).FirstOrDefault();

仅使用 Single()SingleOrDefault()(如果您知道的话)只有一个结果,或者如果有多个结果你想失败。

By using First() or FirstOrDefault()

var query =
    (from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select c.co2Target).FirstOrDefault();

Only use Single() or SingleOrDefault() if you know there is only one result, or if you want to fail if there are multiple results.

抚笙 2024-11-14 14:59:35

您可以使用 Single 扩展方法:

var result =
     (from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select c.co2Target).Single();

其他相关的扩展方法是 SingleOrDefault 第一个 FirstOrDefault

Single 和 First 之间的区别在于,如果查询结果有多个结果,则 Single 会引发异常。如果查询没有返回结果,OrDefault 变体将返回 null,而 Single 和 First 则在不存在结果时抛出异常。

如果您使用的是 Entity Framework 3.5,它不支持 Single,因此您必须使用 First

另一件值得注意的事情是,您的原始代码生成了 IQueryable,这意味着在您评估结果之前它不会实际执行查询。使用任何这些扩展方法都会强制查询立即运行。

You can use the Single extension method:

var result =
     (from c in db.productInfo
     where c.flavor == "Classic Coke" && c.container == "Can"
     select c.co2Target).Single();

Other related extension methods are SingleOrDefault, First and FirstOrDefault.

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 use First.

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.

千纸鹤带着心事 2024-11-14 14:59:35

msdn : SingleOrDefault

使用 Single()SingleOrDefault() 方法获取结果

另请检查:默认扩展方法

msdn : SingleOrDefault

Make use of Single() or SingleOrDefault() method to get result

Also check : Default Extension methods

习惯成性 2024-11-14 14:59:35

如果您的查询始终只返回一个元素作为结果,请使用 SingleOrDefault(),否则如果您的查询结果超过一个元素,则会抛出异常。

(from c in db.productInfo
 where c.flavor == "Classic Coke" && c.container == "Can"
 select c.co2Target).SingleOrDefault();

如果您的结果有多个元素并且您需要其中任何一个,请使用 FirstOrDefualt() 。

(from c in db.productInfo
 where c.flavor == "Classic Coke" && c.container == "Can"
 select c.co2Target).FirstOrDefault();

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.

(from c in db.productInfo
 where c.flavor == "Classic Coke" && c.container == "Can"
 select c.co2Target).SingleOrDefault();

use FirstOrDefualt() if your result more than one element and you need any one of then.

(from c in db.productInfo
 where c.flavor == "Classic Coke" && c.container == "Can"
 select c.co2Target).FirstOrDefault();
眼角的笑意。 2024-11-14 14:59:35
string str = (
  from c in db.productInfo 
  where c.flavor == "Classic Coke" && c.container == "Can"
  select c.co2Target)
    .Single().columnName;
string str = (
  from c in db.productInfo 
  where c.flavor == "Classic Coke" && c.container == "Can"
  select c.co2Target)
    .Single().columnName;
长途伴 2024-11-14 14:59:35

我更喜欢 SingleOrDefault(),如果没有返回任何内容,它不会抛出异常。 MSDN 参考

这样您就可以对此类进行安全保护条件检查案件。

var query = (from c in db.productInfo where c.flavor == "Classic Coke" && c.container == "Can"
                 select c.co2Target).SingleOrDefault();

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.

var query = (from c in db.productInfo where c.flavor == "Classic Coke" && c.container == "Can"
                 select c.co2Target).SingleOrDefault();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文