Linq FirstOrDefault

前端 未结 4 1852
醉话见心
醉话见心 2020-12-06 12:16

I have the following LINQ query:

    Manager mngr = (from tr in DataContext.Manager
                    where tr.Name = \"Jones\").FirstOrDefault();
<         


        
4条回答
  •  星月不相逢
    2020-12-06 12:46

    If you want to count the records, count the result of the query without FirstOrDefault.

    var mngr = (from tr in DataContext.Manager
                    where tr.ID = "2323")
    if(mngr.Count() == 0)
       ....
    else
       Manager manager = mngr.First(); //No need for default since we know we have a record.
    

    You can still run first or default on the mngr to get the specific manager.

提交回复
热议问题