为了账号安全,请及时绑定邮箱和手机立即绑定

Linq Group By 和合并行

Linq Group By 和合并行

C#
MMTTMM 2022-10-15 14:46:53
我有一个类似于以下格式的表格:id | name    | year   | Quality| Location |------------------------------------------1  | Apple   | year1  | Good   | Asia     |2  | Apple   | year2  | Better | Asia     |3  | Apple   | year3  | Best   | Asia     |4  | Apple   | year1  | Best   | Africa   |5  | Apple   | year2  | Bad    | Africa   |6  | Apple   | year3  | Better | Africa   |7  | Apple   | year1  | Best   | Europe   |8  | Apple   | year2  | Bad    | Europe   |9  | Apple   | year3  | Better | Europe   |10 | Orange  | year1  | Bad    | Asia     |11 | Orange  | year2  | Better | Asia     |12 | Orange  | year3  | Bad    | Asia     | 13 | Orange  | year1  | Best   | Africa   |14 | Orange  | year2  | Better | Africa   |15 | Orange  | year3  | Bad    | Africa   |16 | Orange  | year1  | Best   | Europe   |17 | Orange  | year2  | Better | Europe   |18 | Orange  | year3  | Best   | Europe   |19 | Mango   | year1  | Bad    | Asia     |20 | Mango   | year2  | Better | Asia     |21 | Mango   | year3  | Better | Asia     |22 | Mango   | year1  | Good   | Africa   |23 | Mango   | year2  | Better | Africa   |24 | Mango   | year3  | Good   | Africa   |25 | Mango   | year1  | Best   | Europe   |26 | Mango   | year2  | Better | Europe   |27 | Mango   | year3  | Best   | Europe   |我需要 LINQ 中这种格式的列表:{ Location: Asia, year: year1, Good: 1, Bad: 2, Better: 0, Best: 0 }{ Location: Asia, year: year2, Good: 0, Bad: 0, Better: 3, Best: 0 }{ Location: Asia, year: year3, Good: 0, Bad: 1, Better: 1, Best: 1 }....我的 LINQ 查询是这样的:var result = context.Fruits                    .Groupby(f => new { f.Year,f.Location,f.Quality })                    .Select(g => new                    {                        Year = g.Key.Year,                         Location = g.Key.Location,                        Quality = g.Key.Quality,                         Count = g.Count()                    });如何使用 LINQ 获得所需的格式?我是否必须得到结果,然后使用 for each 来得到我需要的格式?
查看完整描述

1 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

您想要Count()该Quality属性,因此将其用作分组中的键确实有意义。如果您只是省略它并仅按年份和位置分组,您将获得所需的输出。


public class Program

{

    public static void Main()

    {

        var fruits = new List<Fruit> {

            new Fruit { Id = 1, Name = "Apple", Year = "year1", Quality = "Good", Location ="Asia"},

            new Fruit { Id = 2, Name = "Apple", Year = "year2", Quality = "Better", Location ="Asia"},

            new Fruit { Id = 3, Name = "Apple", Year = "year3", Quality = "Better", Location ="Asia"},

            new Fruit { Id = 4, Name = "Apple", Year = "year1", Quality = "Best", Location ="Africa"},

            new Fruit { Id = 5, Name = "Orange", Year = "year1", Quality = "Vad", Location ="Asia"},

            new Fruit { Id = 6, Name = "Orange", Year = "year2", Quality = "Better", Location ="Asia"},

            new Fruit { Id = 7, Name = "Orange", Year = "year3", Quality = "Bad", Location ="Asia"},

        };


        var result = fruits.GroupBy(f => new { f.Year,f.Location})

                    .Select(g => new

                    {

                        Year = g.Key.Year, 

                        Location = g.Key.Location,

                        Good = g.Count(x => x.Quality == "Good"),

                        Better = g.Count(x => x.Quality == "Better"),

                        Best = g.Count(x => x.Quality == "Best"),

                    });


        foreach(var line in result) {

            Console.WriteLine(String.Format("Year: {0} - Location: {1} - Good: {2} - Better: {3} - Best: {4}", line.Year, line.Location, line.Good, line.Better, line.Best));

        }

    }

}


public class Fruit

{

    public int Id { get; set; }

    public string Name { get; set; }

    public string Year { get; set; }

    public string Quality { get; set; }

    public string Location { get; set; }

}

输出:


Year: year1 - Location: Asia - Good: 1 - Better: 0 - Best: 0

Year: year2 - Location: Asia - Good: 0 - Better: 2 - Best: 0

Year: year3 - Location: Asia - Good: 0 - Better: 1 - Best: 0

Year: year1 - Location: Africa - Good: 0 - Better: 0 - Best: 1

小提琴:https ://dotnetfiddle.net/eg99at


查看完整回答
反对 回复 2022-10-15
  • 1 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信