如何在Linq MVC5上的同一个select中包含两个对象

时间:2018-01-23 06:16:32

标签: c# linq

我确实有一个复杂的查询来选择一个名为Performance

的完整对象

与其他对象的效果关系是:

  • 效果有一个索引列表
  • 索引有一个SubIndex列表
  • SubIndex有一个指标列表
  • 指标有项目清单

与Spot和Measurement的项目关系:

  • 项目有一个Spot和一个Measurement

下面的查询完全返回我想要的内容,但我想将Spot和Measurement包含在Item对象中。

return _context.Performance.Include(i => i.Indexes
                           .Select(s => s.SubIndexes
                           .Select(d => d.Indicators
                           .Select(t => t.Items))))
                           .SingleOrDefault(p => p.Id == id);

我已经尝试了下面的查询,它正在返回Measurement对象。如何包含Spot对象?

return _context.Performance.Include(i => i.Indexes
                           .Select(s => s.SubIndexes
                           .Select(d => d.Indicators
                           .Select(t => t.Items.Select(tm => tm.Measurement)))))
                           .SingleOrDefault(p => p.Id == id);

1 个答案:

答案 0 :(得分:1)

您也可以添加第二个Include;

return _context.Performance.Include(i => i.Indexes
       .Select(s => s.SubIndexes
       .Select(d => d.Indicators
       .Select(t => t.Items.Select(tm => tm.Measurement)))))
       .Include(i => i.Indexes
       .Select(s => s.SubIndexes
       .Select(d => d.Indicators
       .Select(t => t.Items.Select(tm => tm.Spot)))))
       .SingleOrDefault(p => p.Id == id);