开发者

How do I take the Cartesian join of two lists in c#?

How do I take the Cartesian join of two lists with integer开发者_如何学Pythons in them?

Can this be done with linq?


Assuming you mean a "cross join" or "Cartesian join":

var query = from x in firstList
            from y in secondList
            select new { x, y }

Or:

var query = firstList.SelectMany(x => secondList, (x, y) => new { x, y });

If you want something else (as you can see from comments, the term "cross product" has caused some confusion), please edit your question appropriately. An example would be very handy :)


For curiosity, another way to accomplish this (which produces the same result as Jon Skeet's answer) is:

firstList.Join(secondList, x => true, y => true, (m, n) => new { m, n });


If you're not fond of the query syntax (like myself), MoreLINQ provides a method for it that's cleaner, in my opinion:

using MoreLinq;

// ....

firstList.Cartesian(secondList, (x, y) => new { x, y });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜