开发者

Linq to SQL .Any() with multiple conditions?

开发者 https://www.devze.com 2023-01-29 00:24 出处:网络
I\'m trying to use .Any() in an if statement like so: if(this.db.Users.Any(x => x.UserID == UserID)){

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

if(this.db.Users.Any(x => x.UserID == UserID)){
    // do st开发者_开发技巧uff
}

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?


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消