Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我有两个 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)
不,没有“两种”语法。目前还不清楚你在追求什么。如果你想连接,那么
foreach(var e in b.Concat(d))
如果你想要组合集:
foreach(var e in b.Union(d))
或者两者共有的集合:
foreach(var e in b.Intersect(d))
如果你想要一个交叉加入,那么也许SelectMany. 但坦率地说,为此,嵌套foreach在您的情况下同样合理和有效。
SelectMany
foreach