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?
Well, you can just put multiple "where" clauses in directly, but I don't think you want to. Multiple "where" clauses ends up with a more restrictive filter - I think you want a less restrictive one.
The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.
You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition. AND, OR, and a third operator, NOT, are logical operators.
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.
if(this.db.Users.Any(x => x.UserID == UserID && x.UserName == UserName)){ // do stuff }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With