2011-02-10 51 views
2

您好我創造了這個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。

回答

2

group account部分是說明你想在每個組中使用元素by new { ... }是你想要密鑰爲每個組。這並不侷限於成爲元素中信息的一部分。

作爲一個最簡單的例子,你可能有:

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

這將使你組,其中每組的關鍵是該組中所有代表的人的姓氏,以及每個組的每個元素將是某人的第一個名字。

您可能需要閱讀我的兩個Edulinq的博客文章: