如何将多个连接中的所有字段都包含在Linq语句中?

时间:2016-07-09 10:32:26

标签: c# entity-framework linq linq-to-entities

var largeset =
from invs in context.Invoices
join lines in context.InvoiceLines on invs.InvoiceId equals lines.InvoiceId
join tracks in context.Tracks on lines.TrackId equals tracks.TrackId into grp
select new
          {
              Invoice = invs,
              Detail = grp
          };

在上面的join into语句中,Detail是一个列表,但它只包含Invoice和Track列。我希望能够从InvoiceLine中获取列。

提前致谢。

2 个答案:

答案 0 :(得分:1)

如果您想获取发票的行信息列表,则需要将into移至第一个join并在外部{{1}内执行另一个join }。

这样的事情:

select

答案 1 :(得分:0)

作为Ivan解决方案的替代方案,它应该会产生更好的性能。

var largeset =
  from invs in context.Invoices
  join lines in context.InvoiceLines on invs.InvoiceId equals lines.InvoiceId
  join tracks in context.Tracks on lines.TrackId equals tracks.TrackId
  group new { invs, lines, tracks }
  by new
  {
      invs.InvoiceId,
      invs.InvoiceDate,
      invs.CustomerId,
      invs.Customer.LastName,
      invs.Customer.FirstName
  } into grp
  select new
  {
      InvoiceId = grp.Key.InvoiceId,
      InvoiceDate = grp.Key.InvoiceDate,
      CustomerId = grp.Key.CustomerId,
      CustomerLastName = grp.Key.LastName,
      CustomerFirstName = grp.Key.FirstName,
      CustomerFullName = grp.Key.LastName + ", " + grp.Key.FirstName,
      TotalQty = grp.Sum(l => l.lines.Quantity),
      TotalPrice = grp.Sum(l => l.lines.UnitPrice),
      Tracks = grp.Select(t => t.tracks)
  };