简体   繁体   English

LINQ lambda:如何在没有链接的情况下组合两个表

[英]LINQ lambda: how to combine two tables with no links

I have two tables "Foo" and "Bar".我有两张桌子“Foo”和“Bar”。 I would like to join them on nothing, ie I need it to be a full matrix:我想加入他们,即我需要它是一个完整的矩阵:

Foo

letters字母
a一个
b b
c c

Bar酒吧

numbers数字
1 1
2 2

FooBar富吧

letters字母 numbers数字
a一个 1 1
b b 1 1
c c 1 1
a一个 2 2
b b 2 2
c c 2 2

How do I achieve it using LINQ lambda magic, ie something like如何使用 LINQ lambda 魔术来实现它,即类似

var Foobar = Foo.UnionAll(b => Bar...)...? 

Note both tables are IEnumerable anonymous.请注意,这两个表都是IEnumerable匿名的。

This query should create needed result:此查询应创建所需的结果:

var Foobar = Foo.SelectMany(f => Bar, (f, b) => new { Foo = f, Bar = b});

One option is to use the query syntax of linq:一种选择是使用 linq 的查询语法:

var Foobar = from f in Foo
             from b in Bar
             select new { f.letters, b.numbers };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM