如何在 "boolean conditional statement" 中进行循环?
How can I do a loop within a "boolean conditional statement"?
如何在 C# 的条件语句中放置循环?我在下面有一个例子,它在查询数据时可能很有用,原因有两个:它会避免写出每个项目或围绕整个查询进行 foreaching,如果我们假设查询是,它会避免访问列表中每个项目的数据库查询数据库,我们希望避免增加对数据库的命中。那么如何在条件语句(如 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();
你可以这样做:
var eastTerritorySales =
(from sale in AllSales
where certainTerritorysManagers.Contains(sale.manager)
select sale).ToList();
鉴于 certainTerritorysManagers 是一个字符串列表,sale.manager 需要是一个字符串。
因此,如果管理器是另一个具有属性的 class,您将需要执行以下操作:
where certainTerritorysManagers.Contains(sale.manager.Name)
希望对您有所帮助!
如何在 C# 的条件语句中放置循环?我在下面有一个例子,它在查询数据时可能很有用,原因有两个:它会避免写出每个项目或围绕整个查询进行 foreaching,如果我们假设查询是,它会避免访问列表中每个项目的数据库查询数据库,我们希望避免增加对数据库的命中。那么如何在条件语句(如 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();
你可以这样做:
var eastTerritorySales =
(from sale in AllSales
where certainTerritorysManagers.Contains(sale.manager)
select sale).ToList();
鉴于 certainTerritorysManagers 是一个字符串列表,sale.manager 需要是一个字符串。 因此,如果管理器是另一个具有属性的 class,您将需要执行以下操作:
where certainTerritorysManagers.Contains(sale.manager.Name)
希望对您有所帮助!