Sgraffite
Sgraffite

Reputation: 1928

Linq to SQL .Any() with multiple conditions?

I'm trying to use .Any() in an if statement like so:

if(this.db.Users.Any(x => x.UserID == UserID)){
    // do stuff
}

Is there a way I can put multiple conditions inside of the .Any()? For example something like:

if(this.db.Users.Any(x => x.UserID == UserID AND x.UserName == UserName)){
    // do stuff
}

Or is there a better way to go about this?

Upvotes: 27

Views: 43387

Answers (2)

Cheng Chen
Cheng Chen

Reputation: 43523

if(this.db.Users.Any(x => x.UserID == UserID && x.UserName == UserName)){
    // do stuff
}

Upvotes: 7

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

Sure, use the && operator.

if(this.db.Users.Any(x => x.UserID == UserID && x.UserName == UserName)){
    // do stuff
}

If you can use it in an if statement, you can use it here. The lambda needs to evaluate to a bool.

Upvotes: 44

Related Questions