需要帮助了解Linq group by with join

时间:2011-02-10 14:26:50

标签: linq

您好我创建了这个Linq查询

  var k = from account in _session.All<AccountDetail>()
                join subscriber in _session.All<Subscriber>() on account.ID equals subscriber.AccID
                join subscriberServices in _session.All<SubscriberServce>() on subscriber.ID equals subscriberServices.UserID
                join paymentMethod in _session.All<PaymentMethod>() on subscriberServices.PaymentMethod_ID equals paymentMethod.ID
                join paymentFrequency in _session.All<PaymentFrequency>() on subscriberServices.PaymentFrequency_ID equals paymentFrequency.ID
                group account by new {AccID= account.ID,paymentFrequency= paymentFrequency.Description,paymentMethod= paymentMethod.Description} into G
                select new GenerateInvoiceData() { AccID = G.Key.AccID};

我不明白

group account by new {AccID= account.ID,paymentFrequency= paymentFrequency.Description,paymentMethod= paymentMethod.Description} into G

为什么我在匿名类型中不限制它时指定account,即我可以键入paymentFrequency.Description。

1 个答案:

答案 0 :(得分:2)

group account部分说明了您希望每个组中的元素by new { ... }是您希望每个组的。这并不仅限于成为元素中信息的一部分。

作为一个最简单的例子,你可能有:

from person in people
group person.FirstName by person.LastName

这将为您提供组,其中每个组的键是该组中所有人的姓氏,每个组的每个元素都是某人的名字。

您可能想阅读我的两篇Edulinq博文: