如何在C#中的条件语句中放入循环?我在下面有一个示例,该示例在以下两个方面可能对查询数据很有用:它可以避免将每个项目写出或在整个查询中进行遍历;如果我们假设查询是,则可以避免访问列表中每个项目的数据库。查询数据库,我们希望避免增加对数据库的匹配。因此,如何在条件语句(如if语句或查询语句)中循环。我认为在像Ruby这样的动态语言中这将非常容易,但是在C#或大多数静态语言中没有简单的方法。如果我有任何遗漏,请告诉我。谢谢。
List<string> certainTerritorysManagers = GetTerritoryManagers(east);
var AllSales= GetAllSales();
var EastTerritorySales = (from sale in AllSales
where sale.manager == manager1 || sale.manager == manager2 || ... etc.
// *** Is there a way I can stick a foreach loop in the line above so I don't have to list all of these managers?
// *** Pseudo code might look like: where sale.manager == ANYOF foreach (string manager in aCertainTerritoriesManagers)
// *** If it was all && statements intead of || statements you could use the term ALLOF
select sale).ToList();
最佳答案
我认为您正在寻找Contains
:
var EastTerritorySales =
(from sale in AllSales
where certainTerritoryManagers.Contains(sale.manager)
select sale).ToList();