Linq Group By并合并行

时间:2019-03-07 20:40:00

标签: c# linq group-by

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

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获得所需的格式?我是否必须获取结果,然后使用每个结果才能将其转换为所需的格式?

1 个答案:

答案 0 :(得分:5)

您想要Count()的{​​{1}}属性,因此将其用作分组中的键确实很有意义。如果您仅忽略它,而仅按年份和位置分组,那么您将获得所需的输出。

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

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