c# LINQ: how to retrieve a single result

后端 未结 8 1394
梦如初夏
梦如初夏 2020-12-29 07:00

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.productIn         


        
相关标签:
8条回答
  • 2020-12-29 07:10
    string str = (
      from c in db.productInfo 
      where c.flavor == "Classic Coke" && c.container == "Can"
      select c.co2Target)
        .Single().columnName;
    
    0 讨论(0)
  • 2020-12-29 07:11

    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.

    0 讨论(0)
  • 2020-12-29 07:15

    msdn : SingleOrDefault

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

    Also check : Default Extension methods

    0 讨论(0)
  • 2020-12-29 07:17

    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();
    
    0 讨论(0)
  • 2020-12-29 07:19

    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();
    
    0 讨论(0)
  • 2020-12-29 07:22

    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;
    
    0 讨论(0)
提交回复
热议问题