c# LINQ: how to retrieve a single result

烈酒焚心 提交于 2019-11-30 04:56:07

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;

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();

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.

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.

msdn : SingleOrDefault

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

Also check : Default Extension methods

mBotros

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();
Asif Rehman
string str = (
  from c in db.productInfo 
  where c.flavor == "Classic Coke" && c.container == "Can"
  select c.co2Target)
    .Single().columnName;

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();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!