27

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?

2 Answers 2

44

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Doh! I tried that earlier but I was getting an error, apparently something else was incorrect at the time. Thanks!
How can we specify blocks of conditions. e.g. .Any((x => x.UserID == UserID && x.UserName == UserName) || <<Some condition>> && <<Some condition>>)
@VijayBalkawade just like that. if you can use it in an if statement, then you can use it here.
Will this translate to an Exists? Or a Count > 0?
7
if(this.db.Users.Any(x => x.UserID == UserID && x.UserName == UserName)){ // do stuff } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.