Linq FirstOrDefault

前端 未结 4 1820
醉话见心
醉话见心 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 13:00

    You can use the SingleOrDefault extension method to express that the query only ever should return one result.

    Manager manager = (from tr in DataContext.Manager 
                       where tr.ID = "2323").SingleOrDefault();
    

    However it does not tell you whether you got 0 results or more than 1.

提交回复
热议问题