【发布时间】:2009-01-18 03:41:23
【问题描述】:
我有一个物品清单
- 约翰 ID
- 马特 ID
- 约翰 ID
- 斯科特 ID
- 马特 ID
- 约翰 ID
- 卢卡斯 ID
我想将它们推回到这样的列表中,这也意味着我想按最多的重复项进行排序。
- 约翰 ID 3
- 马特 ID 2
- 斯科特 ID 1
- 卢卡斯 ID 1
让我知道如何使用 LINQ 和 C# 做到这一点。
谢谢大家
EDIT 2 显示代码:
List<game> inventory = new List<game>();
drinkingforDataContext db = new drinkingforDataContext();
foreach (string item in tbTitle.Text.Split(' '))
{
List<game> getItems = (from dfg in db.drinkingfor_Games
where dfg.game_Name.Contains(tbTitle.Text)
select new game
{
gameName = dfg.game_Name,
gameID = Boomers.Utilities.Guids.Encoder.EncodeURLs(dfg.uid)
}).ToList<game>();
for (int i = 0; i < getItems.Count(); i++)
{
inventory.Add(getItems[i]);
}
}
var items = (from xx in inventory
group xx by xx into g
let count = g.Count()
orderby count descending
select new
{
Count = count,
gameName = g.Key.gameName,
gameID = g.Key.gameID
});
lvRelatedGames.DataSource = items;
lvRelatedGames.DataBind();
此查询显示以下结果:
- 1 你好世界时报
- 1 你好世界时报
- 1 Hello World。
- 1 你好世界时报
- 1 你好世界时报
- 1 你好世界时报
- 1 Hello World。
- 1 你好世界时报
它给了我计数和名称,但它没有给我游戏的 ID....
它应该显示:
- 6 你好世界时报 234234
- 2 你好世界。 23432432
【问题讨论】:
-
鉴于您的结果,很明显程序会将您的所有项目视为不同的 - 正如我所说,您需要实现自定义比较器,否则无法选择不同的值