运行两个foreach循环

use*_*952 1 c# loops

如果我有两个foreach循环,如下所示:

foreach(var a in b)
foreach(var c in d)

combining them into single foreach loop
foreach(var e in both b and d)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 11

不,没有"两者兼而有之"的语法.目前还不清楚你是什么之后.如果你想连接,那么

foreach(var e in b.Concat(d))
Run Code Online (Sandbox Code Playgroud)

如果你想要组合集:

foreach(var e in b.Union(d))
Run Code Online (Sandbox Code Playgroud)

或两者共同的集合:

foreach(var e in b.Intersect(d))
Run Code Online (Sandbox Code Playgroud)

如果你想要交叉连接,那么也许SelectMany.但坦率地说,对于你来说,嵌套foreach在你的情况下同样合理有效.