How to group by on 2 child entities and get total of both this child entities?

前端 未结 3 1801
情歌与酒
情歌与酒 2020-11-27 23:32

I want to get total variants run for my Test Version 0 i.e Test Id=100

This is my table and records:

Test:

相关标签:
3条回答
  • 2020-11-28 00:09

    From top of my head, assuming I understood your confusing description. I think you need to union variants from both TestOperation and TestOperationDifference, then distinct and count them. Not sure if this will work in 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;
    

    Also, your naming is confusing. You are using plural (s) for single-item references and you have SourceControlDetailId in your model that is not in table and have SubVariants and SubVariants1 instead of SourceSubVariant and TargetSubVariant. I would recommend to fix this first.

    0 讨论(0)
  • 2020-11-28 00:27

    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();
    
    0 讨论(0)
  • 2020-11-28 00:30
    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();
    
    0 讨论(0)
提交回复
热议问题