var result = from p in list.AsEnumerable() group p by new{p.StatisLlane,p.VehicleClass} into g select new { g.StatisLlane, g.VehicleClass, Avspeed = g.Sum(p => p.Avspeed), Occupancy = g.Sum(p => p.Occupancy), Volume = g.Sum(p => p.Volume), };
List<RadarData> matches; var lineFilter = new[] { 3, 4 }; matches = (from x in list where lineFilter.Contains(x.VehicleClass) group x by new { x.StatisLlane, x.VehicleClass } into temp select new RadarData() { SumAvspeed = temp.Sum(y => y.Avspeed), SumVolume = temp.Sum(y => y.Volume), SumOccupancy = temp.Max(y => y.Occupancy) }).ToList(); if (matches != null && matches.Count > 0) { List<RadarData> matches1; matches1 = (from x in matches group x by x.StatisLlane into temp let divisor = temp.Sum(y => y.Volume) select new RadarData { SumAvspeed = divisor == 0 ? 0 : temp.Sum(y => y.Avspeed * divisor) / divisor, SumVolume = temp.Sum(y => y.Volume), SumOccupancy = temp.Max(y => y.Occupancy) }).ToList(); }针对图中的数据 先筛选一次以后 再进行第2次筛选 发现筛选不出来 不知道问题出在哪里,就是第2个表达式 matches总是为空 或者数据不对。
.GroupBy(i => new { i.StatisLlane, i.VehicleClass })
.Select(r => new Info
{
StatisLlane = r.FirstOrDefault(q => q.StatisLlane != 0).StatisLlane,
Avspeed = r.FirstOrDefault(q => q.Avspeed != 0).Avspeed,
Occupancy = r.FirstOrDefault(q => q.Occupancy != 0).Occupancy,
Volume = r.FirstOrDefault(q => q.Volume != 0).Volume,
VehicleClass = r.FirstOrDefault(q => q.VehicleClass != 0).VehicleClass
})
.ToList();代码写的比较简单,大概看一下意思吧
var result = from p in list.AsEnumerable()
group p by new{p.StatisLlane,p.VehicleClass} into g
select new
{
g.StatisLlane,
g.VehicleClass,
Avspeed = g.Sum(p => p.Avspeed),
Occupancy = g.Sum(p => p.Occupancy),
Volume = g.Sum(p => p.Volume),
};
List<RadarData> matches;
var lineFilter = new[] { 3, 4 };
matches = (from x in list
where lineFilter.Contains(x.VehicleClass)
group x by new { x.StatisLlane, x.VehicleClass }
into temp
select new RadarData()
{
SumAvspeed = temp.Sum(y => y.Avspeed),
SumVolume = temp.Sum(y => y.Volume),
SumOccupancy = temp.Max(y => y.Occupancy) }).ToList();
if (matches != null && matches.Count > 0)
{
List<RadarData> matches1;
matches1 = (from x in matches
group x by x.StatisLlane
into temp
let divisor = temp.Sum(y => y.Volume)
select new RadarData
{
SumAvspeed = divisor == 0 ? 0 : temp.Sum(y => y.Avspeed * divisor) / divisor,
SumVolume = temp.Sum(y => y.Volume),
SumOccupancy = temp.Max(y => y.Occupancy) }).ToList();
}针对图中的数据 先筛选一次以后 再进行第2次筛选 发现筛选不出来 不知道问题出在哪里,就是第2个表达式 matches总是为空 或者数据不对。