C# 如何将带条件的嵌套foreach循环转换为LINQ
我想问一下,是否可以将下面嵌套的foreach循环转换为LINQ表达式C# 如何将带条件的嵌套foreach循环转换为LINQ,c#,linq,C#,Linq,我想问一下,是否可以将下面嵌套的foreach循环转换为LINQ表达式 public interface IFoo { bool IsCorrect(IFoo foo); void DoSomething(IFoo foo); } List<IFoo> listA; //assume that contains items List<IFoo> listB; //assume that contains items foreach (var a in lis
public interface IFoo
{
bool IsCorrect(IFoo foo);
void DoSomething(IFoo foo);
}
List<IFoo> listA; //assume that contains items
List<IFoo> listB; //assume that contains items
foreach (var a in listA)
{
foreach (var b in listB)
{
if (b.IsCorrect(a))
{
b.DoSomething(a);
}
}
}
公共接口IFoo
{
bool-IsCorrect(IFoo-foo);
无效剂量测定(IFoo-foo);
}
清单a//假设包含项目
名单b//假设包含项目
foreach(列表a中的变量a)
{
foreach(列表b中的变量b)
{
如果(b)是正确的(a))
{
b、 剂量测定法(a);
}
}
}
我不确定您到底想做多少,但这是一条Linq语句,它也在做同样的事情:
listA.ForEach(a => listB.ForEach(b =>
{
if (b.IsCorrect(a)) b.DoSomething(a);
}));
我不确定你到底想做多少,但这是一个Linq声明,做同样的事情:
listA.ForEach(a => listB.ForEach(b =>
{
if (b.IsCorrect(a)) b.DoSomething(a);
}));
您可以这样做,但这并没有比目前更有效:
var query= listA.SelectMany(a=>listB.Select(b=>new {a,b}))
.Where(e=>e.b.IsCorrect(e.a))
.ToList()// Foreach is a method of List<T>
.Foreach(e=> e.b.DoSomething(e.a));
然后您的查询将是:
var query= listA.SelectMany(a=>listB.Select(b=>new {a,b}))
.Where(e=>e.b.IsCorrect(e.a))
.Foreach(e=> e.b.DoSomething(e.a));
您可以这样做,但这并没有比目前更有效:
var query= listA.SelectMany(a=>listB.Select(b=>new {a,b}))
.Where(e=>e.b.IsCorrect(e.a))
.ToList()// Foreach is a method of List<T>
.Foreach(e=> e.b.DoSomething(e.a));
然后您的查询将是:
var query= listA.SelectMany(a=>listB.Select(b=>new {a,b}))
.Where(e=>e.b.IsCorrect(e.a))
.Foreach(e=> e.b.DoSomething(e.a));
这应该起作用:
var result =
from a in listA
from b in listB
where b.IsCorrect(a)
select new {a, b};
foreach (var item in result)
item.b.DoSomething(item.a);
这应该起作用:
var result =
from a in listA
from b in listB
where b.IsCorrect(a)
select new {a, b};
foreach (var item in result)
item.b.DoSomething(item.a);
对于方法语法,您可以使用以下查询:
var correctPairs = listA
.SelectMany(a => listB.Where(b => b.IsCorrect(a)).Select(b => new { a, b }));
foreach (var x in correctPairs)
x.b.DoSomething(x.a);
对于方法语法,您可以使用以下查询:
var correctPairs = listA
.SelectMany(a => listB.Where(b => b.IsCorrect(a)).Select(b => new { a, b }));
foreach (var x in correctPairs)
x.b.DoSomething(x.a);
@EtherDragon的可能副本不可能复制@EtherDragon的可能副本不可能复制事实上,这个答案与LINQ无关。@这是真的。我一直认为ForEach方法是linq扩展,但似乎不是!如果您想要一个linq Foreach,这将为您提供:public static分部类functionExtraction{public static void Foreach(this IEnumerable items,Action Action){Foreach(items中的T项)Action(item);}public static IEnumerable Foreach(this IEnumerable items,Func Action){Foreach(items中的T项){收益返回操作(项目);}}@Jim,这将不会涉及比这个方法更多的LINQ。它也不是LINQ风格编程的代表。事实上,它与LINQ风格的编程完全相反。事实上,这个答案与LINQ无关。@itsme86非常正确。我一直认为ForEach方法是LINQ扩展,但它似乎是not!如果您想要一个linq Foreach,这将为您提供:public static分部类functionExtraints{public static void Foreach(this IEnumerable items,Action Action){Foreach(items中的T item)Action(item);}public static IEnumerable Foreach(this IEnumerable items,Func Action){Foreach(items中的T item){收益返回操作(项目);}}@Jim,这种方法不会涉及更多的LINQ。它也不是LINQ风格编程的代表。事实上,它与LINQ风格编程完全相反。不仅这种方法可读,而且它避免了
List.ForEach
,这是一种令人憎恶的方法:-)不仅这种方法可读,而且它避免了List.ForEach
,这是一个令人憎恶的东西:-)