我有一个类似于以下格式的表格:
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 来得到我需要的格式?
斯蒂芬大帝
相关分类