简体   繁体   English

我如何获取linq收到的信息,并在if else语句中使用该信息?

[英]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.

相关问题 如何在C#中从OpenOffice获取性能信息? - How can I take the performance information from OpenOffice in C#? 如何使用 IF 语句中的信息? - How can I use information from IF statements? 如何使用 EF 和 linq 获取包含嵌套信息的实体 - How can I get this entity with nested information using EF and linq 如何使用 LINQ 语句从 SharePoint 获取信息 - How to use a LINQ statement to get information from SharePoint 我希望有一个页面可以从用户那里收集信息,并且我想获取这些信息并在其他页面和类上使用它 - I want to have a page that collects information from the user, and i want to take that information and use it on other pages and classes 如何使用SqlConnection.GetSchema获取同义词信息? - How can I use SqlConnection.GetSchema to get synonym information? 如何在视图中使用来自多个类(表)的信息? - How can I use information from multiple classes (tables) in a view? 我如何使用switch语句而不是太多其他语句 - How Can I use switch statement instead of too many else if 如何使用Linq从集合中选择信息到我制作的另一个具体类? - How to use Linq to select information from a collection into another concrete class I made? 我如何在 LINQ 中使用 SQL 内连接语句作为查询 - How can i use SQL inner join statement as query in LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM