如何重复计数与LINQ列表 [英] How to Count Duplicates in List with LINQ
问题描述
我有一个项目列表
- 约翰·ID
- 马特ID
- 约翰·ID
- 斯科特ID
- 马特ID
- 约翰·ID
- 卢卡斯ID
我要推他们回到像这样一个列表这也意味着我想重复次数最多的排序。
I want to shove them back into a list like so which also means I want to sort by the highest number of duplicates.
- 约翰·ID 3
- 马特ID 2
- 斯科特ID 1
- 卢卡斯ID 1
让我知道我可以使用LINQ和C#这样做。
Let me know how I can do this with LINQ and C#.
感谢所有
编辑2显示code:
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 ......
It gives me the count and name, but it doesn't give me the ID of the game....
它应该显示:
- 6的hello world次234234
- 2的Hello World。 23432432
推荐答案
您可以在按组+排序依据使用。请参见 LINQ 101 详情
You can use "group by" + "orderby". See LINQ 101 for details
var list = new List<string> {"a", "b", "a", "c", "a", "b"};
var q = from x in list
group x by x into g
let count = g.Count()
orderby count descending
select new {Value = g.Key, Count = count};
foreach (var x in q)
{
Console.WriteLine("Value: " + x.Value + " Count: " + x.Count);
}
在回应<一个href=\"http://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list-with-linq#454652\">this帖子(现已删除):
In response to this post (now deleted):
如果你有一些自定义对象的列表,那么你需要使用自定义比较或特定属性组。
If you have a list of some custom objects then you need to use custom comparer or group by specific property.
查询也无法显示的结果。我们展示了完整的code,以获得更好的帮助。
Also query can't display result. Show us complete code to get a better help.
根据您的最新更新:
您有这行code的:
group xx by xx into g
由于xx是一个自定义对象系统不知道如何将一个项目与另一个进行比较。
正如我已经写,你需要引导编译器,并提供一些属性,将在对象比较中使用或提供自定义比较。下面是一个例子:
Since xx is a custom object system doesn't know how to compare one item against another. As I already wrote, you need to guide compiler and provide some property that will be used in objects comparison or provide custom comparer. Here is an example:
请注意,我用的 Foo.Name 作为重点 - 即对象将根据命名属性的值进行分组
Note that I use Foo.Name as a key - i.e. objects will be grouped based on value of Name property.
有一个陷阱 - 你把2个对象被重复的基础上他们的名字,但对于标识?在我的例子我只拿一组中的第一个对象的标识。如果对象具有不同的ID也可以是一个问题。
There is one catch - you treat 2 objects to be duplicate based on their names, but what about Id ? In my example I just take Id of the first object in a group. If your objects have different Ids it can be a problem.
//Using extension methods
var q = list.GroupBy(x => x.Name)
.Select(x => new {Count = x.Count(),
Name = x.Key,
ID = x.First().ID})
.OrderByDescending(x => x.Count);
//Using LINQ
var q = from x in list
group x by x.Name into g
let count = g.Count()
orderby count descending
select new {Name = g.Key, Count = count, ID = g.First().ID};
foreach (var x in q)
{
Console.WriteLine("Count: " + x.Count + " Name: " + x.Name + " ID: " + x.ID);
}
这篇关于如何重复计数与LINQ列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!