在iQueryable上使用Foreach List在第二列表中找到值

问题描述:

我有一个使用查询创建的iQueryable集合,如下所示 -

I have a iQueryable collection created with a query as seen below -

var Results = DataClass.Links.OrderBy(mySel => mySel.Description)
                             .Where(mySel =>                                     mySel.Report_Id.Equals(NumID)                                          
                             new Sections
                             {
                                SectID = mySel.Report_Section.SectID,
                                SectionDesn = mySel.Report_Section.ReportSectionDescription,
                                ReportPageTitle = mySel.tbl_Report.ReportPageTitle,                                                
                                MyUrl = mySel.Link_Url
                             }).Distinct();

我还有一个第二个类型列表 List< string> 调用LookForMe定义为

I also have a second list of type List<string> called LookForMe defined as

var LookForMe = new List<string> { "increase", "abc", "tuesday", "another" };

我现在试图遍历我的结果集合中的每个项目,如果item.URL包含一个

I am now trying to loop through each item in my Results collection and if the item.URL contains one of the values found in my LookForMe list and if so then do something.

foreach (var item in Results)
{
   if (item.URL *contains any of the values in my LookFormE list*)
   {
      // then do something
   }
}

我不知道如何编写if语句来查找当前迭代值是否包含LookForMe列表。任何想法,让我在正确的轨道?

I'm not sure how to write the if statement to find if the current iterated value contains any of the values in the LookForMe list. Any ideas to get me on the right track? Thank you for your time.

您可以试试这个:

foreach (var item in Results)
{
    if (LookForMe.Any(x=>item.URL.Contains(x)) 
    {
        // then do something
    }
}

任何$ c>扩展方法,您正在查找是否存在任何世界在 LookForMe ,它包含在 item.URL 如果是这样,则返回true,否则返回false。

Using the Any extension method, you are looking if there exists any of the worlds in LookForMe, which is included in the item.URL. If so, it returns true. Otherwise, it returns false.