简体   繁体   中英

LINQ query using lambda expression

I have this linq query, which is working fine, but I want to make it shorter by using a lambda expression. Any suggestions or examples might help.

selectedPersons = (from d in entities.PERSONS_DATA
                   where d.PERSON_ID == pid
                   select d).First();
selectedPersons = entities.PERSONS_DATA.First (d => d.PERSON_ID == pid);

If you can use:

electedPersons = entities.PERSONS_DATA.Find(pid);

If there is a chance the pid might not match a row the First will throw an exception. So in this case use:

electedPersons = entities.PERSONS_DATA.FirstOrDefault(d => d.PERSON_ID == pid);

if(electedPersons != null)
   ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM