[英]How to group by on 2 child entities and get total of both this child entities?
我想为我的Test Version 0
即Test 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 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
TestOperationDifference:
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
和TestOperationDifference
,以下是特定Test 100
的3种变体:
Variants1(This run in TestOperation)
Variants2(This run in TestOperation)
Variants3(This run in TestOperationDifference)
这3个以上的父子变量的出现是因为所有这些父子变量都在2个表(即TestOperation和TestOperationDifference)中使用。
因此,为了查找总的父变体,我需要从两个表(TestOperation和TestOperationDifference)中找出对应的子变体,并在此基础上count total parent variants
。
这是我的课:
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; }
}
我的查询:
var data =(from mk in context.Test
select new
{
TotalVariants = (mk.TestOperation.Select(t => t.SubVariants).Count()
+
mk.TestOperationDifference.Select(t => t.SubVariants).Count())
}).ToList();
输出: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
不在表中,并且具有SubVariants
和SubVariants1
而不是SourceSubVariant
和TargetSubVariant
。 我建议先解决此问题。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.