我在 C# 项目中使用 group by 子句执行 Linq 查询时遇到一些问题,如下所示:
public class Stock {
public string Name {get; set;}
public List<int> Values {get; set;}
public float Price {get; set;}
}
// Stock collection
List<Stock> stocks = new List<Stock>()
{
new Stock(){Name="Prod1", Values=new List<int>{1, 2, 3, 0}, Price=5.0f,
new Stock(){Name="Prod1", Values=new List<int>{1, 2, 3, 0}, Price=5.0f,
new Stock(){Name="Prod11", Values=new List<int>{1, 0, 3, 1}, Price=8.0f,
new Stock(){Name="Prod11", Values=new List<int>{1, 0, 3, 1}, Price=8.0f,
new Stock(){Name="Prod18", Values=new List<int>{0, 0, 4, 1}, Price=4.5f,
new Stock(){Name="Prod20", Values=new List<int>{4, 0, 0, 2}, Price=9.9f,
new Stock(){Name="Prod20", Values=new List<int>{4, 0, 0, 2}, Price=9.9f,
new Stock(){Name="Prod29", Values=new List<int>{2, 1, 0, 1}, Price=7.2f,
};
var query = stocks.GroupBy(x => x, (x, g) => new { Count = g.Count(), Values = x}).ToList();
此查询给出错误的分组结果。