我有这个ADO.NET模型:
我在视图中使用MVC以[post]形式请求其中一个报告。 在控制器操作中接收" FormCollection" - 数据我想查询谁返回给我本报告中包含的每个标签及其值。
这是一个想要回归的例子:
Report: A - this is name of report;
{
[0]
{
TagName: TAG1
Value: 0.56 - this value is aggregated value of all values for this tag1
}
[1]
{
TagName: TAG2
Value: 2.45 -> this value is aggregated value of all values for this tag2
}
}
我试图这样做,但没有用:
var report = db.Reports
.Where(r => r.ID == reportID)
.Select(r => new {
r.Name,
r.Tags
})
.ToList();
如何获取此结构中标记的相关值并将其聚合起来?
答案 0 :(得分:1)
如果我理解你是对的,你最终想要得到这个结构:
public class TagWithValue
{
public string Name { get; set; }
public double ValueAggrigated { get; set; }
}
public class RemortTags
{
public string Name { get; set; }
public IEnumerable<TagWithValue> Tags { get; set; }
}
你可以像这样填写这个结构:
var report = db.Reports
.Where(r => r.ID == reportID)
.Select(r => new RemortTags {
Name = r.Name,
Tags = r.Tags.Select(x => new TagWithValue {
Name = x.Name,
ValueAggrigated = x.Sum(y => y.Value1) //that's where you should aggrigate your values. You can use different function if you want
})
})
.ToList();
答案 1 :(得分:0)
解决方案是:
var report = db.Reports.FirstOrDefault(r => r.ID == reportID);
var Tags = report.Tags
.Select(t => new
{
TagName = t.Name,
Values = t.Values
.Where(a => a.TimeStamp > startDate && a.TimeStamp <= endDate)
}).Select(a => new {
Name = a.TagName,
Value = a.Values.Sum(s => s.Value1) })
.ToList();
结果:
感谢所有人的帮助!