问题描述
我对我之前问过的一个类似问题有一个补充.我有一个结构如下的汽车类型列表:
class Car { public string Make { get; set; } public string Model { get; set; } public string SecondHand { get; set; } public int AccidentCount { get; set; } public int MaintenanceCount { get; set; } } List<Car> cars = new List<Car>() { new Car(){Make = "Mercedes", Model = "E-200", SecondHand ="N", AccidentCount = 1 ,MaintenanceCount = 0}, new Car(){Make = "Mercedes", Model = "E-200", SecondHand ="N", AccidentCount = 1 ,MaintenanceCount = 1}, new Car(){Make = "Mercedes", Model = "E-200", SecondHand ="Y", AccidentCount = 1 ,MaintenanceCount = 1}, new Car(){Make = "Mercedes", Model = "E-180", SecondHand ="N", AccidentCount = 0 ,MaintenanceCount = 1}, new Car(){Make = "Mercedes", Model = "E-180", SecondHand ="N", AccidentCount = 1 ,MaintenanceCount = 1} };
我在查询的输出中需要的是 2 列用于 Make 和 Model,通过对它们进行分组,得到 2 列中的 AccidentCount 和 MaintenanceCount 的总和,最后如果给定模型输出"Y"有任何"SecondHand"值"Y"" 否则为 "N".
上面的输出应该是:
Make Model AccidentCount MaintenanceCount SecondHand Mercedes E-200 3 2 Y Mercedes E-180 1 2 N
推荐答案
我在查询输出中需要的是 2 列,通过对它们进行分组,分别用于 Make 和 Model,
cars.GroupBy(c=> new {c.Make,c.Model})<块引用>
在 2 列中获取 AccidentCount 和 MaintenanceCount 的总和
AccidentCount = g.Sum(c=>c.AccidentCount), MaintenanceCount = g.Sum(c=>c.MaintenanceCount)<块引用>
最后,如果给定模型输出"Y"有任何"SecondHand"值"Y",否则为"N".
SecondHand = g.Any(c=> c.SecondHand=="Y") ? "Y" : "N"
最终查询如下所示:
cars.GroupBy(c=> new {c.Make,c.Model}) .Select(g=> new Car { Make = g.Key.Make, Model = g.Key.Model, AccidentCount = g.Sum(c=>c.AccidentCount), MaintenanceCount = g.Sum(c=>c.MaintenanceCount), SecondHand = g.Any(c=> c.SecondHand=="Y") ? "Y" : "N" });
问题描述
I have an addition to a similar question I have asked before. I have a list of car type structured like below :
class Car { public string Make { get; set; } public string Model { get; set; } public string SecondHand { get; set; } public int AccidentCount { get; set; } public int MaintenanceCount { get; set; } } List<Car> cars = new List<Car>() { new Car(){Make = "Mercedes", Model = "E-200", SecondHand ="N", AccidentCount = 1 ,MaintenanceCount = 0}, new Car(){Make = "Mercedes", Model = "E-200", SecondHand ="N", AccidentCount = 1 ,MaintenanceCount = 1}, new Car(){Make = "Mercedes", Model = "E-200", SecondHand ="Y", AccidentCount = 1 ,MaintenanceCount = 1}, new Car(){Make = "Mercedes", Model = "E-180", SecondHand ="N", AccidentCount = 0 ,MaintenanceCount = 1}, new Car(){Make = "Mercedes", Model = "E-180", SecondHand ="N", AccidentCount = 1 ,MaintenanceCount = 1} };
What i need in output of query is 2 columns for Make and Model by grouping them, get sum of AccidentCount and MaintenanceCount in 2 columns and finally if there is any "SecondHand" value"Y" for a given Model output "Y" otherwise "N".
Output for above should be :
Make Model AccidentCount MaintenanceCount SecondHand Mercedes E-200 3 2 Y Mercedes E-180 1 2 N
推荐答案
What i need in output of query is 2 columns for Make and Model by grouping them,
cars.GroupBy(c=> new {c.Make,c.Model})
get sum of AccidentCount and MaintenanceCount in 2 columns
AccidentCount = g.Sum(c=>c.AccidentCount), MaintenanceCount = g.Sum(c=>c.MaintenanceCount)
and finally if there is any "SecondHand" value"Y" for a given Model output "Y" otherwise "N".
SecondHand = g.Any(c=> c.SecondHand=="Y") ? "Y" : "N"
The final query looks like this:
cars.GroupBy(c=> new {c.Make,c.Model}) .Select(g=> new Car { Make = g.Key.Make, Model = g.Key.Model, AccidentCount = g.Sum(c=>c.AccidentCount), MaintenanceCount = g.Sum(c=>c.MaintenanceCount), SecondHand = g.Any(c=> c.SecondHand=="Y") ? "Y" : "N" });