C# LINQ分组求最大、最小、平均值

C# LINQ分组求最大、最小、平均值

 private void GroupBy()
        {
            List<Goods> goods = new List<Goods>()
            {
                new Goods() {Vendor="A",Name="香蕉",Price=3.8},
                new Goods() {Vendor="A",Name="苹果",Price=5.1},
                new Goods() {Vendor="A",Name="梨",Price=10.1},
                new Goods() {Vendor="B",Name="香蕉",Price=3.6},
                new Goods() {Vendor="B",Name="苹果",Price=5.8},
                new Goods() {Vendor="B",Name="梨",Price=9.5}
            };

            var result = (from fruit in goods
                          group fruit by fruit.Name into g
                          let minPrice = g.Min(f => f.Price)
                          select (Name: g.Key, CheapestPrice: minPrice)).ToList();
        }
        public class Goods
        { 
            public string Vendor { get; set; }
            public string Name { get; set; }
            public double Price { get; set; }
        }

你可能感兴趣的:(c#,linq,开发语言)