C# 如何按2个子实体分组,并获取这两个子实体的总数?
我想为我的C# 如何按2个子实体分组,并获取这两个子实体的总数?,c#,asp.net-mvc,entity-framework,linq,C#,Asp.net Mvc,Entity Framework,Linq,我想为我的测试版本0运行总变量,即测试Id=100 这是我的表格和记录: 测试: Id Version 100 0 Id Name Type CategoryId 11 Variant1 Diff 2 12 Variant1 Add 2 13 Variant2 Add 3 14 Variant2 Diff 2 15 Variant3 Add
测试版本0运行总变量,即测试Id=100
这是我的表格和记录:
测试:
Id Version
100 0
Id Name Type CategoryId
11 Variant1 Diff 2
12 Variant1 Add 2
13 Variant2 Add 3
14 Variant2 Diff 2
15 Variant3 Add 6
Id TestId SourceSubVariantId TargetSubVariantId Unmatch
1 100 66 67 0
1 100 67 68 2
1 100 74 75 7
1 100 75 76 0
1 100 77 78 26
变体:
Id Version
100 0
Id Name Type CategoryId
11 Variant1 Diff 2
12 Variant1 Add 2
13 Variant2 Add 3
14 Variant2 Diff 2
15 Variant3 Add 6
Id TestId SourceSubVariantId TargetSubVariantId Unmatch
1 100 66 67 0
1 100 67 68 2
1 100 74 75 7
1 100 75 76 0
1 100 77 78 26
子变体:
Id VariantId Name
66 11 Abc
67 11 PQR
68 11 Xyz
69 12 Abc
70 12 PQR
71 12 Xyz
72 13 Abc
73 13 PQR
74 14 Abc
75 14 PQR
76 14 Xyz
77 15 ABC
78 15 PQR
Id TestId SourceSubVariantId TargetSubVariantId variation
1 100 69 70 0
1 100 70 71 20
1 100 72 73 90
测试操作:
Id VariantId Name
66 11 Abc
67 11 PQR
68 11 Xyz
69 12 Abc
70 12 PQR
71 12 Xyz
72 13 Abc
73 13 PQR
74 14 Abc
75 14 PQR
76 14 Xyz
77 15 ABC
78 15 PQR
Id TestId SourceSubVariantId TargetSubVariantId variation
1 100 69 70 0
1 100 70 71 20
1 100 72 73 90
测试操作差异:
Id Version
100 0
Id Name Type CategoryId
11 Variant1 Diff 2
12 Variant1 Add 2
13 Variant2 Add 3
14 Variant2 Diff 2
15 Variant3 Add 6
Id TestId SourceSubVariantId TargetSubVariantId Unmatch
1 100 66 67 0
1 100 67 68 2
1 100 74 75 7
1 100 75 76 0
1 100 77 78 26
因此,根据上述记录,共有3种变体在2种操作类型上运行,即TestOperation
和TestOperation difference
,以下是特定Test 100
的3种变体:
Variants1(This run in TestOperation)
Variants2(This run in TestOperation)
Variants3(This run in TestOperationDifference)
上述3个父变量将作为一个变量出现,因为所有这些父-子变量都在两个表中使用,即TestOperation和TestOperationDifference。
因此,为了找到总的父变量,我需要从两个表(TestOperation和TestOperationDifference)中找出相应的子变量,并在此基础上,我需要计算总的父变量
这是我的班级:
public class Test
{
public int Id { get; set; }
public string Version { get; set; }
public virtual ICollection<TestOperation> TestOperation { get; set; }
public virtual ICollection<TestOperationDifference> TestOperationDifference { get; set; }
}
public class TestOperation
{
public int Id { get; set; }
public Nullable<int> TestId { get; set; }
public int SourceSubVariantId { get; set; }
public int TargetSubVariantId { get; set; }
public int variation { get; set; }
public virtual SubVariants SubVariants { get; set; }
public virtual SubVariants SubVariants1 { get; set; }
public virtual Test Test { get; set; }
}
public class TestOperationDifference
{
public int Id { get; set; }
public Nullable<int> TestId { get; set; }
public int SourceSubVariantId { get; set; }
public int TargetSubVariantId { get; set; }
public int unmatch { get; set; }
public virtual SubVariants SubVariants { get; set; }
public virtual SubVariants SubVariants1 { get; set; }
public virtual Test Test { get; set; }
}
public class Variants
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public int CategoryId { get; set; }
public virtual ICollection<SubVariants> SubVariants { get; set; }
public virtual Category Category { get; set; }
}
public class SubVariants
{
public int Id { get; set; }
public int VariantId { get; set; }
public string Name { get; set; }
public virtual Variants Variants { get; set; }
public virtual ICollection<TestOperationDifference> TestOperationDifference { get; set; }
public virtual ICollection<TestOperationDifference> TestOperationDifference1 { get; set; }
public virtual ICollection<TestOperation> TestOperation { get; set; }
public virtual ICollection<TestOperation> TestOperation1 { get; set; }
}
产出:8
预期产出:3
更新
好的,这是一个解决方案
var tot_variants_for_test =
(from v_name in
(from t_op in test
select new { v_name = t_op.TestOperation.Select(sv => sv.SubVariants.Variants.Name) }
).First().v_name
select v_name)
.Union(
(from v_name in
(from t_opdf in test
select new { v_name = t_opdf.TestOperationDifference.Select(sv => sv.SubVariants.Variants.Name) }
).First().v_name
select v_name))
.Count();
假设我能理解你那令人困惑的描述,我会发自肺腑。我认为您需要从TestOperation
和TestOperationDifference
中提取变量,然后对它们进行区分和计数。不确定这在EF中是否有效
let toQuery = context.Test.SelectMany(mk=>TestOperation.Select(t=>t.SubVariants.Variants));
let todQuery = context.Test.SelectMany(mk=>TestOperationDifference.Select(t=>t.SubVariants.Variants));
let total = toQuery.Concat(todQuery).Disctinct().Count;
此外,您的命名也很混乱。您对单个项目引用使用复数,并且您的模型中有SourceControlDetailId
,该模型不在表中,并且有子变量
和子变量1
,而不是源子变量
和目标子变量
。我建议先解决这个问题。但是我做了同样的事情,并且得到了我在问题中提到的错误输出。这两个查询都会给我输出8,但这不是预期的输出。请查看预期的输出谢谢你的回答你真的应该努力缩小问题的规模。你真的需要5张表格来展示你面临的问题吗?你应该简化情况,直到它只是相关部分(但仍然完整)@JonSkeet:事实上,我把这个表格放在这里是为了便于理解这个问题,如果没有这个表格,就很难确切知道需要什么样的输出,但我同意问题的规模很大,你是说你认为不可能进一步简化这个问题吗?你真的需要那么多的表和属性吗?这对我来说似乎不太可能。我添加了一个Init方法,以使问题更容易验证。。更新仅在同行修订后可见。我在下面的回答中发布了静态方法。非常感谢你的回答非常感谢你的回答,但是你能告诉我为什么你使用selectmany而不是select吗???@学习如果你知道它们之间的区别,这是显而易见的。
from test in Tests
where version == 0
let opsVariants = test.TestOperations
.SelectMany(x => x.SourceSubVariant.Variant).Distinct()
let diffsVariants = test.TestOperationDifferences
.SelectMany(x => x.SourceSubVariant.Variant).Distinct()
let variants = opsVariants.Union(diffsVariants)
select variants.Count();