如何计算与LINQ列表中的重复

我有一个项目列表

  • 约翰
  • 马特ID
  • 约翰
  • Scott ID
  • 马特ID
  • 约翰
  • 卢卡斯ID

我想把它们推回到这样的列表中,这也意味着我想按照最大数量的重复进行sorting。

  • 约翰ID 3
  • 马特ID 2
  • 斯科特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世界时间你好
  • 1世界时间你好
  • 1 Hello World。
  • 1世界时间你好
  • 1世界时间你好
  • 1世界时间你好
  • 1 Hello World。
  • 1世界时间你好

它给我的数量和名称,但它不给我的游戏的ID ….

它应该显示:

  • 6你好世界时间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); } 

回复这个post (现在删除):

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

还查询不能显示结果。 向我们展示完整的代码以获得更好的帮助。

根据您的最新更新:

你有这行代码:

 group xx by xx into g 

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

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

有一个问题 – 你根据他们的名字把两个对象重复处理,但是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); } 

使用方法链稍微缩短版本:

 var list = new List<string> {"a", "b", "a", "c", "a", "b"}; var q = list.GroupBy(x => x) .Select(g => new {Value = g.Key, Count = g.Count()}) .OrderByDescending(x=>x.Count); foreach (var x in q) { Console.WriteLine("Value: " + x.Value + " Count: " + x.Count); } 

其他解决scheme使用GroupByGroupBy很慢(它拥有内存中的所有元素),所以我写了我自己的方法CountBy

 public static Dictionary<TKey,int> CountBy<TSource,TKey>(this IEnumerable<TSource> source, Func<TSource,TKey> keySelector) { var countsByKey = new Dictionary<TKey,int>(); foreach(var x in source) { var key = keySelector(x); if (!countsByKey.ContainsKey(key)) countsByKey[key] = 0; countsByKey[key] += 1; } return countsByKey; } 

你也可以做字典:

  var list = new List<string> { "a", "b", "a", "c", "a", "b" }; var result = list.GroupBy(x => x) .ToDictionary(y=>y.Key, y=>y.Count()) .OrderByDescending(z => z.Value); foreach (var x in result) { Console.WriteLine("Value: " + x.Key + " Count: " + x.Value); } 

这里是完整的程序请检查这个

 static void Main(string[] args) { List<string> li = new List<string>(); li.Add("Ram"); li.Add("shyam"); li.Add("Ram"); li.Add("Kumar"); li.Add("Kumar"); var x = from obj in li group obj by obj into g select new { Name = g.Key, Duplicatecount = g.Count() }; foreach(var m in x) { Console.WriteLine(m.Name + "--" + m.Duplicatecount); } Console.ReadLine(); }