beaudetious
beaudetious

Reputation: 2416

FirstOrDefault with Multiple Conditions

In Link to Sql, this works fine:

User user = this.dataContext.Users.FirstOrDefault(p => p.User_ID == loginID);

However, I would like to search using conditions like:

User user = this.dataContext.Users.FirstOrDefault(
     p => p.User_ID == 250 && p => p.UserName == "Jack");

What's the correct way to do this?

Thanks.

Upvotes: 31

Views: 49705

Answers (1)

Femaref
Femaref

Reputation: 61437

var user = this.dataContext.Users.FirstOrDefault(
     p => p.User_ID == 250 && p.UserName == "Jack");

The p => at the beginning counts for the whole expression. The syntax used here is a shorthand for

(p) =>
      {
         return p.User_ID == 250 && p.UserName == "Jack";
      }

Upvotes: 58

Related Questions