2012-02-29 24 views
2

我有一個List的對象,看起來財產以後這樣的GroupBy,然後訂購基於組在C#中的另一個列表

class Item 
{ 
    int ItemId; 
    string Name; 
    DateTime CreatedDate; 
    ... more properties, but not relevant ... 
} 

,另一種是持有票

class Vote 
{ 
    int ItemId; 
    ... more properties, but not relevant ... 
} 

所以投票點的項目。我已經收集了所有選票從數據庫中,並保存他們在一個列表

var votes = GetAllVotes().ToLookup(v => v.ItemId).OrderByDescending(v => v.Count()); 

現在我有給我的itemId的得票最多在一個良好的名單列表。有沒有辦法讓我根據選票清單對我的物品清單進行分類?

var items = GetAllItems().OrderBy(i => i.CreatedDate); 
//then I want to do something like this: 
items.OrderBy(votes.Select(v => v.Key, v.Count()) 

因此,這裏的目標是物品按票數排序,所有0票的物品按日期排序。

回答

3

這聽起來像你想要的東西,如:

from item in GetAllItems() 
join vote in GetAllVotes() on item.ItemId equals vote.VoteId into votes 
orderby votes.Count() descending, item.CreatedDate 
select new { Item = item, Votes = votes.Count() }; 

(我可以,如果你想要把在非查詢表達符號,但它不會像整齊。)

+0

是。這正是我想要的,但我已經知道了,不是嗎?在我甚至提出這個問題之前,你可能在記事本上寫下了幾分鐘的答案。 – peirix 2012-02-29 09:57:44

相關問題