如何使用LINQ to SQL执行CROSS JOIN?
交叉连接只是两组笛卡尔积.它没有明确的连接运算符.
var combo = from p in people from c in cars select new { p.Name, c.Make, c.Model, c.Colour };
用同样的事情linq
扩展方法:
var names = new string[] { "Ana", "Raz", "John" }; var numbers = new int[] { 1, 2, 3 }; var newList=names.SelectMany( x => numbers, (y, z) => { return y + z + " test "; }); foreach (var item in newList) { Console.WriteLine(item); }
根据史蒂夫的回答,最简单的表达方式是:
var combo = from Person in people from Car in cars select new {Person, Car};
A Tuple
是笛卡尔积的良好类型:
public static IEnumerable> CrossJoin (IEnumerable sequence1, IEnumerable sequence2) { return sequence1.SelectMany(t1 => sequence2.Select(t2 => Tuple.Create(t1, t2))); }