c# - 如何对 2 个子实体进行分组并获取这两个子实体的总计?

标签 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      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

测试操作差异:

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 种类型的操作上运行,即 TestOperationTestOperationDifference,下面是特定 Test 100 的 3 个变体:

Variants1(This run in TestOperation)
Variants2(This run in TestOperation)
Variants3(This run in TestOperationDifference)

上述 3 个父变体将出现,因为所有这些父子变体都在 2 个表中使用,即 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; }
        }

我的查询:

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

最佳答案

更新

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

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();

关于c# - 如何对 2 个子实体进行分组并获取这两个子实体的总计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38120664/

相关文章:

c# - 我们可以在领域驱动设计中使用 ASP.NET Identity 吗?

c# - 没有PK的表中记录的数据不正确

c# - 处理对数据库的重复 linq 查询的正确做法应该是什么?

oracle - 实体数据模型向导在尝试添加新的 Oracle 连接时出现异常

c# - 获取可用(语言)resx 文件的列表

C#获取XML属性

c# - 当 WCF 服务运行批处理文件时,XCopy 或 MOVE 不起作用。为什么?

c# - 从客户端检测到具有潜在危险的 Request.QueryString 值

asp.net-mvc - 通过 ajax 发送请求时自动呈现部分

asp.net-mvc - 重定向到 HTTPS 错误 - ASP.NET MVC