Как сгруппировать нижеприведенную таблицу с помощью запроса 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 в этом слоте,
напримервывод должен быть
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 не может использовать этозапрос или возможность конвертировать то же самое.
Любая помощь, пожалуйста ..