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

                    });


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


MMTTMM
浏览 119回答 1
1回答

斯蒂芬大帝

您想要Count()该Quality属性,因此将其用作分组中的键确实有意义。如果您只是省略它并仅按年份和位置分组,您将获得所需的输出。public class Program{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var fruits = new List<Fruit> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Fruit { Id = 1, Name = "Apple", Year = "year1", Quality = "Good", Location ="Asia"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Fruit { Id = 2, Name = "Apple", Year = "year2", Quality = "Better", Location ="Asia"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Fruit { Id = 3, Name = "Apple", Year = "year3", Quality = "Better", Location ="Asia"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Fruit { Id = 4, Name = "Apple", Year = "year1", Quality = "Best", Location ="Africa"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Fruit { Id = 5, Name = "Orange", Year = "year1", Quality = "Vad", Location ="Asia"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Fruit { Id = 6, Name = "Orange", Year = "year2", Quality = "Better", Location ="Asia"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Fruit { Id = 7, Name = "Orange", Year = "year3", Quality = "Bad", Location ="Asia"},&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; var result = fruits.GroupBy(f => new { f.Year,f.Location})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(g => new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Year = g.Key.Year,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Location = g.Key.Location,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Good = g.Count(x => x.Quality == "Good"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Better = g.Count(x => x.Quality == "Better"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Best = g.Count(x => x.Quality == "Best"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; foreach(var line in result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(String.Format("Year: {0} - Location: {1} - Good: {2} - Better: {3} - Best: {4}", line.Year, line.Location, line.Good, line.Better, line.Best));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class Fruit{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public string Year { get; set; }&nbsp; &nbsp; public string Quality { get; set; }&nbsp; &nbsp; public string Location { get; set; }}输出:Year: year1 - Location: Asia - Good: 1 - Better: 0 - Best: 0Year: year2 - Location: Asia - Good: 0 - Better: 2 - Best: 0Year: year3 - Location: Asia - Good: 0 - Better: 1 - Best: 0Year: year1 - Location: Africa - Good: 0 - Better: 0 - Best: 1小提琴:https ://dotnetfiddle.net/eg99at
打开App,查看更多内容
随时随地看视频慕课网APP