var groupResults = from gr in models where gr.Noise != 0 && gr.Temperature != 0 && gr.Humidity != 0 && gr.Pm25 != 0 group gr by new { gr.IP, gr.Name, gr.Port } into g select new { g.Key.IP, g.Key.Name, g.Key.Port, NoiseAvg = g.Average(m => m.Noise), NoiseMax = g.Max(m => m.Noise), NoiseMin = g.Min(m => m.Noise), TemperatureAvg = g.Average(m => m.Temperature), TemperatureMax = g.Max(m => m.Temperature), TemperatureMin = g.Min(m => m.Temperature), HumidityAvg = g.Average(m => m.Humidity), HumidityMax = g.Max(m => m.Humidity), HumidityMin = g.Min(m => m.Humidity), Pm25Avg = g.Average(m => m.Pm25), Pm25Max = g.Max(m => m.Pm25), Pm25Min = g.Min(m => m.Pm25), CollectTime = g.FirstOrDefault().CreatedTime }; var groupResults2 = models.Where(m => m.Noise != 0 && m.Temperature != 0 && m.Humidity != 0 && m.Pm25 != 0) .GroupBy(m => new { m.IP, m.Name, m.Port }).Select(g => new { g.Key.IP, g.Key.Name, g.Key.Port, NoiseAvg = g.Average(m => m.Noise), NoiseMax = g.Max(m => m.Noise), NoiseMin = g.Min(m => m.Noise), TemperatureAvg = g.Average(m => m.Temperature), TemperatureMax = g.Max(m => m.Temperature), TemperatureMin = g.Min(m => m.Temperature), HumidityAvg = g.Average(m => m.Humidity), HumidityMax = g.Max(m => m.Humidity), HumidityMin = g.Min(m => m.Humidity), Pm25Avg = g.Average(m => m.Pm25), Pm25Max = g.Max(m => m.Pm25), Pm25Min = g.Min(m => m.Pm25), CollectTime = g.FirstOrDefault().CreatedTime, });
两者效果一样