Eri*_*tin 2 c# loops conditional-statements
如何在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();
Run Code Online (Sandbox Code Playgroud)
我想你正在寻找Contains:
var EastTerritorySales =
(from sale in AllSales
where certainTerritoryManagers.Contains(sale.manager)
select sale).ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
88 次 |
| 最近记录: |