【问题标题】:Linq Group By and merge rowsLinq Group By 和合并行
【发布时间】:2019-07-29 20:25:23
【问题描述】:

我有一个类似于以下格式的表格:

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()
                    });

这给了我一些类似的东西:

{ Location: Asia, year: year1, Quality: Good, Count: 1 }
{ Location: Asia, year: year1, Quality: Bad, Count: 2 }
.
.
.
.

如何使用 LINQ 获得所需的格式?我是否必须得到结果,然后使用 for each 来得到我需要的格式?

【问题讨论】:

    标签: c# linq group-by


    【解决方案1】:

    您想要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

    【讨论】:

    • 完美!!正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2014-05-20
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多