0

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();
2
  • 1
    I disagree. I believe the below is more readable. This is more of a preference than fact. Commented Feb 4, 2016 at 3:33
  • That query already is using a lambda expression. Commented Feb 4, 2016 at 3:41

2 Answers 2

2
selectedPersons = entities.PERSONS_DATA.First (d => d.PERSON_ID == pid);
Sign up to request clarification or add additional context in comments.

3 Comments

why single? why not first?
You are correct, it should be first. Updated answer.
Thank you so much...didn't thought it would be this simple :)
0

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)
   ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.