[英]How can I take the information I received with linq and use that information in an if else statement?
I have this LINQ
that works just fine: 我有这个
LINQ
可以正常工作:
var checkIFExist = (from row in db.Following
where follow.UserName == checkUserNameInLinq
&& follow.FollowThisUser == FollowUsername
&& follow.isFollowing == true
select row).ToList();
If all values are correct, I would like to use them in an if else statement. 如果所有值都正确,我想在if else语句中使用它们。 for example:
例如:
if (checkIFExist == ALL 3 VALUES IN LINQ IS CORRECT)
{
Index();
}
Your linq query returns rows which meet your conditions: 您的linq查询返回满足您条件的行:
var rowsMeetingCriteria = from row in db.Following
where (... conditions ...)
select row;
Now, on which condition do you want to perform an action? 现在,您要在哪种条件下执行操作? If you want to do something if there's at least one row retunred by the query, use .Any() method:
如果要在查询返回至少一行的情况下执行某项操作,请使用.Any()方法:
if (rowsMeetingCriteria.Any())
{
// code
}
If you have any other needs, take a look at methods available for Enumerables here . 如果您还有其他需求,请在此处查看可用于Enumerables的方法 。 You can also put your query inside the Any method, like below, but when the query has few parts you should probably do it separately as above (better readability):
您也可以将查询放入Any方法中,如下所示,但是当查询的部分很少时,您可能应该像上面分别进行操作(更好的可读性):
if (rowsMeetingCriteria.Any(row => row.Field1 == ... && row.Field2 > ...)
{
// code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.