Linq Группирует по и объединяет строки

#c# #linq #group-by

#c# #linq #группировать по

Вопрос:

У меня есть таблица, похожая на следующий формат:

 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:

Вы хотите использовать 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

Комментарии:

1. Идеально!! Именно то, что я хотел!!