使用linq进行表聚合(计算平均值)

时间:2018-06-06 13:19:15

标签: linq entity-framework-6 linq-to-entities aggregate aggregation

如何使用linq查询聚合下表

Date                tagname                                 value
06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4)   54.73497
06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(5)   3.417564
06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4)   94.82829
06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4)   15.08091
06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(5)   3.6422
06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4)   5.078211
06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4)   68.00956
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(5)   94.6864
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(4)   32.43211
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(4)   65.16206
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(5)   81.18947
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(4)   4.419947
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(4)   95.77668
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(5)   10.43907
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(4)   79.12902
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(4)   62.20364
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(5)   97.43433
06-06-2018 14:30:00 Poll.Registers Block 0.310-PT-304_(4)   25.74978
06-06-2018 14:45:00 Poll.Registers Block 0.310-PT-304_(5)   50.49747
06-06-2018 14:45:00 Poll.Registers Block 0.310-PT-304_(4)   65.33123
06-06-2018 14:45:00 Poll.Registers Block 0.310-PT-304_(4)   18.90912
06-06-2018 14:45:00 Poll.Registers Block 0.310-PT-304_(5)   55.9916
06-06-2018 14:45:00 Poll.Registers Block 0.310-PT-304_(4)   23.86106
06-06-2018 14:45:00 Poll.Registers Block 0.310-PT-304_(4)   18.72116
06-06-2018 14:45:00 Poll.Registers Block 0.310-PT-304_(5)   0.06596069

预期结果应该像

每个时间段只有不同的标记名,其中avegate值进入该插槽,

e.g。 输出应该是

06-06-2018 14:15:00  Poll.Register Block 0.310-PT-304(4) "Value should be avg"
06-06-2018 14:15:00  Poll.Register Block 0.310-PT-304(5) "Value should be avg"
06-06-2018 14:30:00  Poll.Register Block 0.310-PT-304(4) "Value should be avg"
06-06-2018 14:30:00  Poll.Register Block 0.310-PT-304(5) "Value should be avg"
06-06-2018 14:45:00  Poll.Register Block 0.310-PT-304(4) "Value should be avg"
06-06-2018 14:45:00  Poll.Register Block 0.310-PT-304(5) "Value should be avg"

---编辑 - 这些是我试过的几个问题..

var g = (from x in ObjEntities.TagDataValues
                             where x.ValueDateTime >= FromDate && x.ValueDateTime <= EndDate && MachineName.Contains(x.MachineName) && ServerName.Contains(x.ServerName) && Tags.Contains(x.TagName)

                             select new
                             {
                                 TagName = x.TagName,
                                 MachineName = x.MachineName,
                                 ServerName = x.ServerName,
                                 TagValue = x.TagValue,
                                 DtTime = x.ValueDateTime
                             }).ToList().GroupBy(cd => new
                             {
                                 date = cd.DtTime.AddSeconds(-cd.DtTime.Second).AddMinutes(-cd.DtTime.Minute % 15),
                                 tagname = cd.TagName,
                                 tagvalue = cd.TagValue
                             }).ToList().Select(o => new
                             {
                                 Date = o.Key.date,
                                 tagname = o.Key.tagname,
                                 value = o.Key.tagvalue
                             }).ToList().GroupBy(tr => tr.tagname).Select(x => new {
                                TagName = x.Key,
                                Value = x.Average(gf => gf.value),
                                Date = x.Select(gf => gf.Date).Distinct()
                             }).ToList();





var g = (from x in ObjEntities.TagDataValues
                             where x.ValueDateTime >= FromDate && x.ValueDateTime <= EndDate && MachineName.Contains(x.MachineName) && ServerName.Contains(x.ServerName) && Tags.Contains(x.TagName)

                             select new
                             {
                                 TagName = x.TagName,
                                 MachineName = x.MachineName,
                                 ServerName = x.ServerName,
                                 TagValue = x.TagValue,
                                 DtTime = x.ValueDateTime
                             }).ToList().GroupBy(cd => new
                             {
                                 date = cd.DtTime.AddSeconds(-cd.DtTime.Second).AddMinutes(-cd.DtTime.Minute % 15),
                                 tagname = cd.TagName,
                                 tagvalue = cd.TagValue
                             }).ToList().Select(o => new
                             {
                                 Date = o.Key.date,
                                 tagname = o.Key.tagname,
                                 value = o.Key.tagvalue
                             }).ToList().GroupBy(tr => new {
                                TagName = tr.tagname,
                                DateTime = tr.Date
                             } ).Select(x => new {
                                TagName = x.Key,
                                Value = x.Average(gf => gf.value),
                                Date = x.Key.DateTime
                             }).ToList();

我是LINQ的新手,因此无法获得正确的结果..

我已经为它编写了sql查询,它给出了完美的结果

declare @StartDate DateTime = CAST('06/06/2018 14:26:56' AS datetime) declare @EndDate DateTime = CAST('06/06/2018 14:32:56' AS datetime) SELECT CONVERT(nvarchar, ValueDateTime, 113) as MINUTE , avg(TagDataValue.TagValue) Value, TagName FROM TagDataValue WHERE ValueDateTime >=  @StartDate AND ValueDateTime <= @EndDate and TagName in ('Poll.Registers Block 0.310-PT-304_(4)','Poll.Registers Block
0.310-PT-304_(5)') GROUP BY CONVERT(nvarchar, ValueDateTime, 113) , TagName

此查询提供了完美的结果,但由于linq中的代码无法使用此查询或能够转换相同的查询。

任何帮助请...

1 个答案:

答案 0 :(得分:0)

按DateTimes分组总是很棘手,因为生成SQL代码时C#上可用的许多函数都不可用,并且可能会导致问题。

由于我无法重现您的环境,我创建了一些仅基于linq的示例。您可能必须使用TruncateTime重新创建,以便它在数据库级别完全运行。

var g = entityList
                .Where(x => x.ValueDateTime >= FromDate && x.ValueDateTime <= ToDate && MachineNames.Contains(x.MachineName))
                .Select(x => new
                {
                    quarterDateTime = x.ValueDateTime
                                       .AddSeconds(-x.ValueDateTime.Second)
                                       .AddMinutes(-x.ValueDateTime.Minute % 15),
                    x.MachineName,
                    x.Value
                })
                .GroupBy( x => new { x.quarterDateTime, x.MachineName })
                .Select( x => new {  x.Key.quarterDateTime, x.Key.MachineName, AverageValue = x.Average(p => p.Value) })
                .OrderBy( x => x.quarterDateTime )
                .ToList();

我想说,可能第一个Select和GroupBy可以合并,但为了更好的可读性,我离开了。

有关TruncateTime的详细信息,请参阅this