如何通过组2子实体,并得到双方总这个孩子实体? [英] How to group by on 2 child entities and get total of both this child entities?
问题描述
我想获得的总变种运行我的测试版本0
即测试ID = 100
这是我的表和记录:
测试:
ID版本
100 0
变体:
ID名称类型的CategoryId
11 VARIANT1 DIFF 2
12 VARIANT1添加2
13 VARIANT2添加3
14 VARIANT2 DIFF 2
15 VARIANT3添加6
SubVariants
标识VariantId名称
66 11美国广播公司
67 11 PQR
68 11 XYZ69 12美国广播公司
70 12 PQR
71 12 XYZ72 13美国广播公司
73 13 PQR74 14美国广播公司
75 14 PQR
76 14 XYZ77 15 ABC
78 15 PQR
TestOperation
标识TestId SourceSubVariantId TargetSubVariantId变化
1 100 69 70 0
1 100 70 71 20
1 100 72 73 90
TestOperationDifference:
标识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
类型的操作即<$ C $运行C> TestOperation 和 TestOperationDifference
以下是具体的3个变种测试100
:
Variants1(此运行在TestOperation)
Variants2(此运行在TestOperation)
Variants3(此运行在TestOperationDifference)
由于这一切的父子变种在2个表即 TestOperation和TestOperationDifference使用上面这3父变种会来的。
所以,寻找总父变种我需要从两个表(TestOperation和TestOperationDifference)计算出的使用和基于相应的子变种上,我需要算总父变种
。
这是我的类:
公共类测试
{
公众诠释标识{搞定;组; }
公共字符串版本{搞定;组; }
公共虚拟的ICollection&LT; TestOperation&GT; TestOperation {搞定;组; }
公共虚拟的ICollection&LT; TestOperationDifference&GT; TestOperationDifference {搞定;组; }
} 公共类TestOperation
{
公众诠释标识{搞定;组; }
公众可空&LT; INT&GT; TestId {搞定;组; }
公众诠释SourceSubVariantId {搞定;组; }
公众诠释TargetSubVariantId {搞定;组; }
公众诠释的变化{搞定;组; }
公共虚拟SubVariants SubVariants {搞定;组; }
公共虚拟SubVariants SubVariants1 {搞定;组; }
公共虚拟测试测试{搞定;组; } } 公共类TestOperationDifference
{
公众诠释标识{搞定;组; }
公众可空&LT; INT&GT; TestId {搞定;组; }
公众诠释SourceSubVariantId {搞定;组; }
公众诠释TargetSubVariantId {搞定;组; } 公众诠释UNMATCH {搞定;组; } 公共虚拟SubVariants SubVariants {搞定;组; }
公共虚拟SubVariants SubVariants1 {搞定;组; }
公共虚拟测试测试{搞定;组; }
} 公共类变体
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; }
公共字符串类型{搞定;组; }
公众诠释的CategoryId {搞定;组; } 公共虚拟的ICollection&LT; SubVariants&GT; SubVariants {搞定;组; } 公共虚拟分类分类{搞定;组; }
} 公共类SubVariants
{
公众诠释标识{搞定;组; }
公众诠释VariantId {搞定;组; }
公共字符串名称{;组; } 公共虚拟变种变种{搞定;组; }
公共虚拟的ICollection&LT; TestOperationDifference&GT; TestOperationDifference {搞定;组; } 公共虚拟的ICollection&LT; TestOperationDifference&GT; TestOperationDifference1 {搞定;组; }
公共虚拟的ICollection&LT; TestOperation&GT; TestOperation {搞定;组; }
公共虚拟的ICollection&LT; TestOperation&GT; TestOperation1 {搞定;组; }
}
我的查询:
=(从MK在context.Test VAR数据
新选择
{ TotalVariants =(mk.TestOperation.Select(T =&GT; t.SubVariants).Count之间的()
+
mk.TestOperationDifference.Select(T =&GT; t.SubVariants).Count之间的())
})了ToList()。
输出:8
预计产量:3
块引用>任何人可以帮助我解决这个???
很抱歉,如果问题似乎是这么大。
解决方案更新的
学习好,这里是一个解决方案。
VAR tot_variants_for_test =
(从v_name在
(从测试t_op
选择新{v_name = t_op.TestOperation.Select(SV =&GT; sv.SubVariants.Variants.Name)}
)。首先()。v_name
选择v_name)
。联盟(
(从v_name在
(从测试t_opdf
选择新{v_name = t_opdf.TestOperationDifference.Select(SV =&GT; sv.SubVariants.Variants.Name)}
)。首先()。v_name
选择v_name))
。计数();I want to get total variants run for my
Test Version 0
i.eTest Id=100
This is my table and records:
Test:
Id Version 100 0
Variants:
Id Name Type CategoryId 11 Variant1 Diff 2 12 Variant1 Add 2 13 Variant2 Add 3 14 Variant2 Diff 2 15 Variant3 Add 6
SubVariants:
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
TestOperation:
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
So from the above records there are
total 3 variants
run on2
types of operation i.eTestOperation
andTestOperationDifference
and below are the 3 variants for specificTest 100
:Variants1(This run in TestOperation) Variants2(This run in TestOperation) Variants3(This run in TestOperationDifference)
This above 3 parent variants will come as because all this parent child variants are being used in 2 tables i.e TestOperation and TestOperationDifference.
So for finding total parent variants i need to figure out from both the tables(TestOperation and TestOperationDifference) corresponding child variants are used and based on that i need to
count total parent variants
.This is my class:
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; } }
My query:
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();
Output :8
Expected output:3
Can anybody help me with this???
Sorry if question seems to be so big.
解决方案Update
Ok Learning, here it is a solution..
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();
这篇关于如何通过组2子实体,并得到双方总这个孩子实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!