2012-02-29 33 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