如何使用LINQ在列表中计数重复(How to Count Duplicates in List with LINQ)

编程入门 行业动态 更新时间:2024-10-25 06:28:44
如何使用LINQ在列表中计数重复(How to Count Duplicates in List with LINQ)

我有一个项目列表

约翰编号 马特ID 约翰编号 Scott ID 马特ID 约翰编号 卢卡斯ID

我想把它们推回到这样的列表中,这也意味着我想按最多的重复排序。

约翰·ID 3 马特ID 2 Scott ID 1 卢卡斯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 times 1 hello world times 1你好世界 1 hello world times 1 hello world times 1 hello world times 1你好世界 1 hello world times

它给我的计数和名字,但它不给我的游戏的ID ....

应该显示:

6 hello world times 234234 2你好世界 23432432

I have a list of items

John ID Matt ID John ID Scott ID Matt ID John ID Lucas 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.

John ID 3 Matt ID 2 Scott ID 1 Lucas ID 1

Let me know how I can do this with LINQ and C#.

Thanks All

EDIT 2 Showing 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();

This query displays these results:

1 hello world times 1 hello world times 1 Hello World. 1 hello world times 1 hello world times 1 hello world times 1 Hello World. 1 hello world times

It gives me the count and name, but it doesn't give me the ID of the game....

It should display:

6 hello world times 234234 2 Hello World. 23432432

最满意答案

您可以使用“group by”+“orderby”。 有关详细信息,请参见LINQ 101

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); }

回覆这篇文章 (现已删除):

如果您有一些自定义对象的列表,那么您需要按照特定属性使用自定义比较器或组。

也查询不能显示结果。 告诉我们完整的代码以获得更好的帮助。

根据您的最新更新:

你有这一行代码:

group xx by xx into g

由于xx是一个自定义对象系统,不知道如何比较一个项目与另一个项目。 正如我已经写的,你需要指导编译器,并提供一些属性,用于比较对象或提供自定义比较器。 这是一个例子:

请注意,我使用Foo.Name作为键 - 即对象将根据Name属性的值进行分组。

有一个catch - 你根据他们的名字来对待2个对象是重复的,但是Id呢? 在我的例子中,我只是把一个组中的第一个对象的ID。 如果您的对象具有不同的ID,那么这可能是一个问题。

//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); }

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); }

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.

Also query can't display result. Show us complete code to get a better help.

Based on your latest update:

You have this line of code:

group xx by xx into g

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:

Note that I use Foo.Name as a key - i.e. objects will be grouped based on value of Name property.

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); }

更多推荐

本文发布于:2023-07-15 21:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   列表中   LINQ   List   Count

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!