简体   繁体   English

Linq to SQL一对多关系加入

[英]Linq to SQL one to many to many relationship join

I have trouble making joins. 我无法加入。 Overall query is going to be huge, but basicly, user inputs 1 ID, and data for report is going to be populated from database. 总体查询将是巨大的,但是基本上,用户输入1个ID,并且报告数据将从数据库中填充。

There can be one to many relationship. 可能存在一对多的关系。 I made ... join oneToManyTable_ in db.tableA on ID equals oneToManyTable_.DeviceId into oneToManyTable . ... join oneToManyTable_ in db.tableA on ID equals oneToManyTable_.DeviceId into oneToManyTable This expression returns IEnumerable. 该表达式返回IEnumerable。 Trouble now - each of those entities I have to join more tables. 现在麻烦了-每个实体我都必须加入更多表。 How do I do that? 我怎么做? If i write ... join anotherTable in db.tableB on oneToManyTable.ID equals anotherTable.ID into oneToManyTable , it yells to me, that oneToManyTable is IEnumerable. 如果我写... join anotherTable in db.tableB on oneToManyTable.ID equals anotherTable.ID into oneToManyTable ,它对我大叫,oneToManyTable是IEnumerable的。 Hope you could understand my trouble. 希望你能理解我的麻烦。

It was easier if you could have been more specific. 如果您可以更具体一些,这会更容易。 Anyway, I think your problem is that you don't use select after your first join. 无论如何,我认为您的问题是您第一次加入后不使用select The format should be like this: 格式应如下所示:

var output =
from class1 in context.Table1
join class2 in context.Table2 
    on class1.ID equals class2.DeviceID
select new { Class1 = class1, Class2 = class2 } into firstJoin
join class3 in context.Table3 
    on firstJoin.Class1.ID equals class3.ID
select new { firstJoin, Class3 = class3 }

This will join your three classes on their ID and also give you all their properties in output. 这将使您的三个类加入其ID,并在输出中提供它们的所有属性。

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

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