我有以下两种对象类型
public class A
{
public int a;
public int b;
public int c;
}
public class B
{
public int d;
public string e;
public string f;
}
我有两个以上两个类别的清单,如下所示
List<A> lista=new List<A>();
lista.Add(new A(){a=10, b=20, c=30});
lista.Add(new A(){a=40, b=50, c=60});
lista.Add(new A(){a=70, b=80, c=90});
List<B> listb=new List<B>();
listb.Add(new B(){d=100, e="hello1", f="world1"});
listb.Add(new B(){d=200, e="hello2", f="world2"}});
现在,我想使用选定的字段创建上述2个列表的交叉联接 例如:
[{a=10,b=20,d=100,e="hello1"}, //First item in lista with 1st item in listb
{a=10,b=20,d=200,e="hello2"} //second item in listb with 1st item in lista
{a=40,b=50,d=100,e="hello1"} //Second Item in lista with 1st item in listb
{a=40,b=50,d=200,e="hello2"} //Second Item in lista with 2nd item in listb
{a=70,b=80,d=100,e="hello1"} //Third Item in lista with 1st item in listb
{a=70,b=80,d=200,e="hello2"} //Third Item in lista with 2nd item in listb
]
我不愿意在LINQ中如何做到这一点,否则编写foreach循环是唯一的选择吗?
答案 0 :(得分:0)
像这样吗?
lista.SelectMany(o=>listb, (o, c)=> new {
o.a, o.b, c.d, c.e
});
答案 1 :(得分:0)
以linq查询语法:
var cartesianProduct = from a in lista
from b in listb
select new { a.a, a.b, b.d, b.e };