我如何在EF中加入以下内容?表之间没有关系,没有外键。
Select t1.ID, t1.firstname, t2.ID,t2.name from MY_TEST_TABLE1 t1, MY_TEST_TABLE2 t2 where t1.firstname = t2.name
您可以这样做:
var query= from t1 in context.MY_TEST_TABLE1 from t2 in context.MY_TEST_TABLE2 where t1.firstname == t2.name select new { Table1Id= t1.ID, FirstName= t1.firstname, Table2Id=t2.ID,Name= t2.name};
在Linq to Entities中进行交叉联接的另一种方法是使用SelectMany扩展方法:
var query= context.MY_TEST_TABLE1.SelectMany( t1=>context.MY_TEST_TABLE2 .Where(t2=>t1.firstname == t2.name) .Select(t2=>new { Table1Id= t1.ID, FirstName= t1.firstname, Table2Id=t2.ID, Name= t2.name }) );