我定义了一个上下文,该上下文公开了一个带有“结果”子集合的“记录”。每个结果都有一个ResultType,每个Record可以具有每种类型的多个结果;
public class Record : Entity<int>
{
public int Count { get; set; }
public virtual List<Result> Results { get; set; } = new List<Result>();
}
public class Result : Entity<int>
{
public int RecordId { get; set; }
public Record Record { get; set; }
public ResultType ResultType { get; set; }
public string Value1 { get; set; }
public string Value2 { get; set; }
}
我希望返回由ResultType按Result.Value1和Result.Value2的组合数据分组/求和的所有记录的列表,总和为Record.Count。
例如
Record 1 : Count - 10
- Result (ResultType = 1,Value1 = "value1",Value2 = "value2")
- Result (ResultType = 1,Value1 = "other1",Value2 = "other2")
- Result (ResultType = 0,Value1 = "a",Value2 = "b")
Record 2 : Count - 10
- Result (ResultType = 1,Value2 = "b")
Record 3 : Count - 50
- Result (ResultType = 1,Value2 = "value2)
- Result (ResultType = 0,Value2 = "b)
将给出2行,并带有结果的组合值;
Count = 20,CombinedResultType0 = "a#b",CombinedResultType1 = "value1|value2,other1|other2"
Count = 50,CombinedResultType1 = "value1|value2"
我正在使用Linq to Entities,但无法弄清楚如何对单个或多个语句进行隐式化和分组。目前,我通过在插入数据时创建数据的复合视图(CombinedResultType0 / 1)并将其添加到Record中,然后仅按该列分组而不存储每个“结果”,来解决此问题。不幸的是,这不能为我提供一种直接查询结果的干净方法。