如何使用Linq获得最高价值

时间:2022-11-27 17:15:04

Let's say I have following data:

假设我有以下数据:

Name    Priority
A       3
A       5
B       1
C       1
C       3
C       2

I want to get list of distinct names with highest priority, so the result would look like:

我想获得具有最高优先级的不同名称列表,因此结果如下所示:

Name    Priority
A       5
B       1
C       3

How can I use Linq to do that?

我如何使用Linq来做到这一点?

3 个解决方案

#1


17  

var query = yourData
    .GroupBy(x => x.Name,
             (k, g) => g.Aggregate((a, x) => (x.Priority > a.Priority) ? x : a));

// and a quick test...
foreach (var result in query)
{
    Console.WriteLine(result.Name + " " + result.Priority);
}

#2


5  

Here's an alternative approach:

这是另一种方法:

var items = new List<Tuple<string, int>>()
{
    Tuple.Create("A", 3),
    Tuple.Create("A", 5),
    Tuple.Create("B", 1),
    Tuple.Create("C", 1),
    Tuple.Create("C", 3),
    Tuple.Create("C", 2)
};

var results = items.GroupBy(i => i.Item1)
                   .SelectMany(g => g
                       .Where(i => i.Item2 == g.Max(m => m.Item2)))
                   .Distinct();

Or if you prefer using the C# LINQ syntax:

或者,如果您更喜欢使用C#LINQ语法:

results = (from item in items
           group item by item.Item1 into groupedItems
           let maxPriority = groupedItems.Max(item => item.Item2)
           from element in groupedItems
           where element.Item2 == maxPriority
           select element).Distinct();

#3


2  

Another simple approach without aggregating

另一种没有聚合的简单方法

        var listNP = new List<NP>()
          {
              new NP() {name="A",priority=3},
              new NP() {name="A",priority=5},
              new NP() {name="b",priority=1},
              new NP() {name="b",priority=1},
              new NP() {name="c",priority=3},
              new NP() {name="c",priority=2},
          };

          var np = listNP.GroupBy(x => x.name).Select(y => new
          {
              name = y.Key,
              max =  y.Max(x=>x.priority)

          }).ToList();

Update:

更新:

var np = listNP.GroupBy(x => x.name)
           .Select(y => y.OrderByDescending(z => z.priority).First()).ToList();

#1


17  

var query = yourData
    .GroupBy(x => x.Name,
             (k, g) => g.Aggregate((a, x) => (x.Priority > a.Priority) ? x : a));

// and a quick test...
foreach (var result in query)
{
    Console.WriteLine(result.Name + " " + result.Priority);
}

#2


5  

Here's an alternative approach:

这是另一种方法:

var items = new List<Tuple<string, int>>()
{
    Tuple.Create("A", 3),
    Tuple.Create("A", 5),
    Tuple.Create("B", 1),
    Tuple.Create("C", 1),
    Tuple.Create("C", 3),
    Tuple.Create("C", 2)
};

var results = items.GroupBy(i => i.Item1)
                   .SelectMany(g => g
                       .Where(i => i.Item2 == g.Max(m => m.Item2)))
                   .Distinct();

Or if you prefer using the C# LINQ syntax:

或者,如果您更喜欢使用C#LINQ语法:

results = (from item in items
           group item by item.Item1 into groupedItems
           let maxPriority = groupedItems.Max(item => item.Item2)
           from element in groupedItems
           where element.Item2 == maxPriority
           select element).Distinct();

#3


2  

Another simple approach without aggregating

另一种没有聚合的简单方法

        var listNP = new List<NP>()
          {
              new NP() {name="A",priority=3},
              new NP() {name="A",priority=5},
              new NP() {name="b",priority=1},
              new NP() {name="b",priority=1},
              new NP() {name="c",priority=3},
              new NP() {name="c",priority=2},
          };

          var np = listNP.GroupBy(x => x.name).Select(y => new
          {
              name = y.Key,
              max =  y.Max(x=>x.priority)

          }).ToList();

Update:

更新:

var np = listNP.GroupBy(x => x.name)
           .Select(y => y.OrderByDescending(z => z.priority).First()).ToList();