【问题标题】:How can I use order by and group by in Linq Extraction Method and With multiple data?如何在 Linq 提取方法和多个数据中使用 order by 和 group by?
【发布时间】:2017-01-03 14:09:24
【问题描述】:

我有一个人列表,其中包含一个人的体重、年龄、城市和姓名属性。

Person.cs:

 public class Person
{
public int Weight { get; set; }
public int Age { get; set; }
public string Name {get; set;}
public string City {get; set;}
}

程序.cs:

 List<Person> personList = new List<Person>();

 personList.Add(new Person { Age = 15, Weight = 68, Name = “Dany”, City =”New York”  });
 personList.Add(new Person { Age = 19, Weight = 75, Name = “Dany”, City =”New York” });
 personList.Add(new Person { Age = 17, Weight = 68, Name = “Dany”, City =”New York” });
 personList.Add(new Person { Age = 15, Weight = 55, Name = “Dany”, City =”New York” });
 personList.Add(new Person { Age = 15, Weight = 53, Name = “Dany”, City =”New York” });
 personList.Add(new Person { Age = 17, Weight = 88, Name = “Dany”, City =”New York” });
 personList.Add(new Person { Age = 19, Weight = 45, Name = “Dany”, City =”New York” });
 personList.Add(new Person { Age = 20, Weight = 88, Name = “Dany”, City =”New York” });

 //Grouped person list 
 List<People> GroupedPersonList = new List<People>();

 var GroupedPersonList = personList.GroupBy(x => x.Age , (key,element) =>    new
{
    Age = key,
    Weight = element.Max(c=>c.Weight)
    City = ????,
    Name = ?????
});

应列出的元素:按年龄分组,每个组应选择权重最高的。(我想按年龄排序此列表,但该列表仅包含该年龄最重的人。然后我将创建一个新列表通过这个。)新的新列表应该是这样的:

(GroupedPersonList 内容)=>

年龄 |重量 |名称 |城市

-------|------- |------|--------

15 | 68 |丹妮 |纽约

17 | 88 |丹妮 |纽约

19 | 75 |丹妮 |纽约

20 | 88 |丹妮 |纽约

  foreach (var item in GroupedPersonList)
  {
    string city =  item.City; // Error because No city in list
  }

我可以这样分组。但我只能使用 2 个属性(年龄和体重)。我还需要分配这些参数。如何分配其他参数?如何在 LINQ 提取方法中做到这一点?

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    我相信你想要的是:

    var GroupedPersonList = personList
        .OrderByDescending(x => x.Weight)
        .GroupBy(x=>x.Age)
        .Select(x =>x.First())
        .OrderBy (x => x.Age);
    

    它的作用是获取您的原始列表并且:

    • 按重量降序排列。
    • 按年龄分组。这些组仍将按重量降序排列。
    • 选择每个组中的第一个项目,因为排序将是重量最高的项目。
    • 按年龄排序结果列表,这是所需的输出。

    这假设GroupedPersonList 应该是List&lt;Person&gt;,尽管您确实提到它是List&lt;People&gt;,所以我可能误解了一些东西。如果People 真的是一个对象,那么请告诉我们它的样子!

    此外,您的最终列表将引用与原始列表相同的对象,因此如果您更改了这些对象上的任何内容,它将反映在原始对象中。这可能是也可能不是您想要的。然而,这确实意味着您可以访问 Person 对象上的每个属性,而不必像您在代码中尝试那样单独设置它们。

    【讨论】:

      【解决方案2】:
      var GroupedPersonList = personList.GroupBy(x => x.Age, (key, element) =>
      {
          var weightiest = element.First(p => p.Weight == element.Max(c => c.Weight));
          return new
                  {
                      Age = key,
                      Weight = weightiest.Weight,
                        City = weightiest.City,
                      Name = weightiest.Name
                  };
      });
      

      【讨论】:

      • 如果你在 {} 中扭曲它,一个 linq 函数可以做多于一行的代码,就像 If 或 forEach 一样。首先,我从所有同龄人中找到体重最大的人(第一个体重如何),并将其放在最重的位置。然后我使用年龄(键)和最重的对象属性创建并返回一个新对象。
      【解决方案3】:
      var GroupedPersonList = personList.GroupBy(
          x => x.Age,
          (k, x) => x.OrderByDescending(p => p.Weight).First());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-06
        • 2011-06-28
        • 1970-01-01
        • 2011-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多