您如何在LINQ中编码交叉联接?
问题描述:
我的应用程序中有一个过程返回两个不同的对象列表.两个列表中的对象属于同一类型.我需要将两个列表交叉连接在一起以形成一个集成列表.如果是SQL,则可能是这样的:
I have a process in my application that returns two different lists of objects. The objects in both lists are of the same type. I need to cross join the two lists together to form one integrated list. If it were SQL, it'd be something like this:
SELECT A.KEY, A.DATA AS DATA1, B.DATA AS DATA2
FROM TABLE1 AS A
CROSS JOIN TABLE2
这是纯LINQ,因为数据是在实际的通用List 集合中返回的.
This is pure LINQ as the data is returned in actual generic List<> collections.
此操作的LINQ语法是什么?
What is the LINQ syntax for this operation?
托尼
答
var res = from t1 in table1
from t2 in table2
select new
{
KEY = t1.KEY,
DATA1 = t1.DATA,
DATA2 = t2.DATA,
};