How do I take the Cartesian join of two lists with integers 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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With