如何对 2 个子实体进行分组并获得这两个子实体的总数? [英] How to group by on 2 child entities and get total of both this child entities?

查看:18
本文介绍了如何对 2 个子实体进行分组并获得这两个子实体的总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的 Test Version 0Test Id=100

运行总变体

这是我的表和记录:

测试:

Id 版本100 0

变体:

Id 名称类型 CategoryId11 Variant1 Diff 212 变体 1 添加 213 变体 2 添加 314 Variant2 Diff 215 Variant3 添加 6

子变体:

Id VariantId 名称66 11 美国广播公司67 11 PQR68 11 多维数据集69 12 美国广播公司70 12 PQR71 12 多维数据集72 13 美国广播公司73 13 PQR74 14 美国广播公司75 14 PQR76 14 多维数据集77 15 ABC78 15 PQR

测试操作:

Id TestId SourceSubVariantId TargetSubVariantId 变体1 100 69 70 01 100 70 71 201 100 72 73 90

TestOperationDifference:

Id TestId SourceSubVariantId TargetSubVariantId 不匹配1 100 66 67 01 100 67 68 21 100 74 75 71 100 75 76 01 100 77 78 26

因此,从上述记录中,共有 3 个变体运行在 2 种操作类型上,即 TestOperationTestOperationDifference,以下是特定 Test 100 的 3 个变体:

Variants1(在TestOperation中运行)变体2(在TestOperation中运行)变体3(在TestOperationDifference中运行)

出现上述 3 个父变体是因为所有这些父子变体都在 2 个表中使用,即 TestOperation 和 TestOperationDifference.

因此,为了找到总父变体,我需要从两个表(TestOperation 和 TestOperationDifference)中找出对应的子变体,并基于此我需要计算总父变体.

这是我的课:

公共类测试{公共 int Id { 获取;放;}公共字符串版本 { 获取;放;}公共虚拟 ICollection<TestOperation>测试操作{得到;放;}公共虚拟 ICollectionTestOperationDifference { 得到;放;}}公共类测试操作{公共 int Id { 获取;放;}public Nullable测试 ID { 获取;放;}公共 int SourceSubVariantId { 获取;放;}公共 int TargetSubVariantId { 获取;放;}公共 int 变体 { 得到;放;}public virtual SubVariants SubVariants { get;放;}public virtual SubVariants SubVariants1 { get;放;}公共虚拟测试测试{获取;放;}}公共类 TestOperationDifference{公共 int Id { 获取;放;}public Nullable测试 ID { 获取;放;}公共 int SourceSubVariantId { 获取;放;}公共 int TargetSubVariantId { 获取;放;}公共 int 不匹配 { 得到;放;}public virtual SubVariants SubVariants { get;放;}public virtual SubVariants SubVariants1 { get;放;}公共虚拟测试测试{获取;放;}}公共类变体{公共 int Id { 获取;放;}公共字符串名称 { 获取;放;}公共字符串类型{获取;放;}公共 int 类别 ID { 获取;放;}公共虚拟 ICollection子变体 { 得到;放;}公共虚拟类别类别{获取;放;}}公共类子变量{公共 int Id { 获取;放;}公共 int VariantId { 获取;放;}公共字符串名称 { 获取;放;}公共虚拟变体变体{获取;放;}公共虚拟 ICollectionTestOperationDifference { 得到;放;}公共虚拟 ICollectionTestOperationDifference1 { 得到;放;}公共虚拟 ICollection<TestOperation>测试操作{得到;放;}公共虚拟 ICollection<TestOperation>TestOperation1 { 得到;放;}}

我的查询:

var data =(from mk in context.Test选择新的{TotalVariants = (mk.TestOperation.Select(t => t.SubVariants).Count()+mk.TestOperationDifference.Select(t => t.SubVariants).Count())}).ToList();

<块引用>

输出:8

预期输出:3

解决方案

更新

好的学习,这是一个解决方案..

var tot_variants_for_test =(从 v_name 中(来自测试中的 t_op选择新 { v_name = t_op.TestOperation.Select(sv => sv.SubVariants.Variants.Name) }).First().v_name选择 v_name).联盟((从 v_name 中(来自测试中的 t_oppdf选择新 { v_name = t_opdf.TestOperationDifference.Select(sv => sv.SubVariants.Variants.Name) }).First().v_name选择 v_name)).数数();

I want to get total variants run for my Test Version 0 i.e Test 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 on 2 types of operation i.e TestOperation and TestOperationDifference and below are the 3 variants for specific Test 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

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆