使用LINQ获取包括重复项的前n个值

huangapple go评论88阅读模式
英文:

Get top n values including duplicates with LINQ

问题

以下是翻译好的部分:

请考虑这个列表:

  1. 城市
  2. ---------------------
  3. 城市1 10
  4. 城市2 20
  5. 城市3 30
  6. 城市4 40
  7. 城市5 50
  8. 城市6 60

如果我想要获取前3个城市中的最高值,我可以编写以下的LINQ查询:

  1. MyList.OrderByDescending(o=>o.Value).Take(3);

现在考虑这个列表:

  1. 城市
  2. ---------------------
  3. 城市1 10
  4. 城市2 20
  5. 城市3 30
  6. 城市4 40
  7. 城市5 50
  8. 城市6 60
  9. 城市7 60

我想要一个查询,返回所有具有前3个最高值的城市。对于上述列表,我想要以下结果:

  1. 城市
  2. ---------------------
  3. 城市7 60
  4. 城市6 60
  5. 城市5 50
  6. 城市4 40

谢谢。

英文:

Please consider this list:

  1. City Value
  2. ---------------------
  3. City1 10
  4. City2 20
  5. City3 30
  6. City4 40
  7. City5 50
  8. City6 60

If I want to get top 3 Values in Cities I can write this LINQ:

  1. MyList.OrderByDescending(o=>o.Value).Take(3);

Now consider this list:

  1. City Value
  2. ---------------------
  3. City1 10
  4. City2 20
  5. City3 30
  6. City4 40
  7. City5 50
  8. City6 60
  9. City7 60

I want a query that return all cities with top 3 highest values. For above list I want this result:

  1. City Value
  2. ---------------------
  3. City7 60
  4. City6 60
  5. City5 50
  6. City4 40

Thanks

答案1

得分: 14

This list will contain all cities with the top 3 highest values, sorted in descending order by value.

英文:
  1. var result = MyList.GroupBy(o => o.Value)
  2. .OrderByDescending(g => g.Key)
  3. .Take(3)
  4. .SelectMany(g => g)
  5. .ToList();

This list will contain all cities with the top 3 highest values, sorted in descending order by value.

答案2

得分: 0

A bit clunky, this solution finds the third highest DISTINCT value, then returns all values equal or greater to it.

  1. using System.Diagnostics.CodeAnalysis;
  2. var MyList = new List<(string, string)>();
  3. MyList.Add(("City1", "10"));
  4. MyList.Add(("City2", "20"));
  5. MyList.Add(("City3", "30"));
  6. MyList.Add(("City4", "40"));
  7. MyList.Add(("City5", "50"));
  8. MyList.Add(("City6", "60"));
  9. MyList.Add(("City7", "60"));
  10. var topThreeDistinct = MyList.Distinct(new StringStringComparer()).OrderByDescending(o => o.Item2).Take(3).ToList();
  11. var thirdValue = topThreeDistinct[2].Item2;
  12. Console.WriteLine($"Third value = {thirdValue}");
  13. var topThreeAll = MyList.Where(o => o.Item2.CompareTo(thirdValue) >= 0 ).ToList();
  14. foreach (var item in topThreeAll)
  15. Console.WriteLine(item);
  16. Console.WriteLine("FINISHED");
  17. class StringStringComparer : IEqualityComparer<(string, string)>
  18. {
  19. public bool Equals((string, string) x, (string, string) y)
  20. {
  21. if (x.Item2 == y.Item2)
  22. {
  23. return true;
  24. }
  25. else
  26. {
  27. return false;
  28. };
  29. }
  30. public int GetHashCode([DisallowNull] (string, string) obj)
  31. {
  32. return Convert.ToInt32(obj.Item2);
  33. }
  34. }

(C# .NET 6.0 Console App)

英文:

A bit clunky, this solution finds the third highest DISTINCT value, then returns all values equal or greater to it.

  1. using System.Diagnostics.CodeAnalysis;
  2. var MyList = new List&lt;(string, string)&gt;();
  3. MyList.Add((&quot;City1&quot;, &quot;10&quot;));
  4. MyList.Add((&quot;City2&quot;, &quot;20&quot;));
  5. MyList.Add((&quot;City3&quot;, &quot;30&quot;));
  6. MyList.Add((&quot;City4&quot;, &quot;40&quot;));
  7. MyList.Add((&quot;City5&quot;, &quot;50&quot;));
  8. MyList.Add((&quot;City6&quot;, &quot;60&quot;));
  9. MyList.Add((&quot;City7&quot;, &quot;60&quot;));
  10. var topThreeDistinct = MyList.Distinct(new StringStringComparer()).OrderByDescending(o =&gt; o.Item2).Take(3).ToList();
  11. var thirdValue = topThreeDistinct[2].Item2;
  12. Console.WriteLine($&quot;Third value = {thirdValue}&quot;);
  13. var topThreeAll = MyList.Where(o =&gt; o.Item2.CompareTo(thirdValue) &gt;= 0 ).ToList();
  14. foreach (var item in topThreeAll)
  15. Console.WriteLine(item);
  16. Console.WriteLine(&quot;FINISHED&quot;);
  17. class StringStringComparer : IEqualityComparer&lt;(string, string)&gt;
  18. {
  19. public bool Equals((string, string) x, (string, string) y)
  20. {
  21. if (x.Item2 == y.Item2)
  22. {
  23. return true;
  24. }
  25. else
  26. {
  27. return false;
  28. };
  29. }
  30. public int GetHashCode([DisallowNull] (string, string) obj)
  31. {
  32. return Convert.ToInt32(obj.Item2);
  33. }
  34. }

(C# .NET 6.0 Console App)

huangapple
  • 本文由 发表于 2023年4月19日 16:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定