c# LINQ: how to retrieve a single result

后端 未结 8 1457
梦如初夏
梦如初夏 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: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();
    

提交回复
热议问题