我有一个项目列表
- John ID
- Matt ID
- John ID
- Scott ID
- Matt ID
- John ID
- Lucas ID
我想将它们放回到一个列表中,就像这样排序也意味着我想按最高数量的重复项进行排序。
- John ID 3
- Matt ID 2
- Scott ID 1
- Lucas ID 1
请告诉我如何使用LINQ和C#完成这个任务。
谢谢大家
编辑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 次“hello world”
- 1 次“hello world”
- 1 个“Hello World.”
- 1 次“hello world”
- 1 次“hello world”
- 1 次“hello world”
- 1 个“Hello World.”
- 1 次“hello world”
它给出了计数和名称,但没有给出游戏的ID....
它应该显示:
- 6 次“hello world” 234234
- 2 个“Hello World.” 23432432