[英]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. 我有这个linq查询,它工作正常,但是我想通过使用lambda表达式使其更短。 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. 如果有可能pid可能与一行不匹配,则First会抛出异常。 So in this case use:
因此,在这种情况下,请使用:
electedPersons = entities.PERSONS_DATA.FirstOrDefault(d => d.PERSON_ID == pid);
if(electedPersons != null)
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.