Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用LINQ表达式替换foreach_C#_Linq_Select_Foreach - Fatal编程技术网

C# 如何用LINQ表达式替换foreach

C# 如何用LINQ表达式替换foreach,c#,linq,select,foreach,C#,Linq,Select,Foreach,可能重复: 假设我有以下类和对象: class Product { public int ProductId { get; set; } public string ProductDesc { get; set; } } string[] arrayString = new string []{"A", "B", "C", "D"}; List<Product> products = new List<product>(); 如果不是这样,如何使用

可能重复:

假设我有以下对象

class Product
{ 
   public int ProductId { get; set; }
   public string ProductDesc { get; set; }
} 

string[] arrayString = new string []{"A", "B", "C", "D"};

List<Product> products = new List<product>();

如果不是这样,如何使用LINQ重写第一个代码段

否,等效LINQ形式为

var filteredProducts = products.Where(p =>
    arrayString.Any(s => p.ProblemDesc.Contains(s));
大声说:

选择
arrayString
包含在产品的
ProblemDesc


不,等效的LINQ形式是

var filteredProducts = products.Where(p =>
    arrayString.Any(s => p.ProblemDesc.Contains(s));
大声说:

选择
arrayString
包含在产品的
ProblemDesc


如果你的“产品”只有描述“问题”的字段,我想我不想要你跟踪的产品:)哈哈,我想用我的真实案例,但后来我发现大多数人都熟悉产品类别如果你的“产品”只有描述“问题”的字段,我想我不想要你跟踪的产品:)哈哈,我想用我的真实案例,但后来我发现大多数人都熟悉这个产品。非常感谢,非常感谢你的口头表达explanation@Jon您的解决方案很可能解决了CiccioMiami实际想要知道的问题,但它并不等同于他的第一个
foreach
示例,因为他正在重新分配厕所内的
过滤器产品
。不确定这是否是样本中的一个错误,即他是否在他指定的
foreach
中执行某些任务omitted@ntziolis:当然,这不难看出。该示例代码可能有点误导,但不难猜出这里的想法是什么。我刚刚注意到我的错误,我将更新该问题,以防其他用户会面临相同的问题!非常感谢,真的很感激你的口头表达explanation@Jon您的解决方案很可能解决了CiccioMiami实际想要知道的问题,但它并不等同于他的第一个
foreach
示例,因为他正在loo内部重新分配
filteredProducts
。不确定这是否是样本中的一个错误,即他是否在他指定的
foreach
中执行某些任务omitted@ntziolis:当然,这不难看出。该示例代码可能有点误导,但不难猜出这里的想法是什么。我刚刚注意到我的错误,我将更新该问题,以防其他用户会面临相同的问题!
var filteredProducts = products.Where(p =>
    arrayString.Any(s => p.ProblemDesc.Contains(s));