繁体   English   中英

无法访问具有group by的字段和使用linq的多个连接

[英]Not able to access fields with group by and multiple joins with linq

我想仅列出那些平均值与记录不同的类别。

这种差异存在于两个表中: TestOperation,TestOperationDifference

我想计算以下3个字段的平均值:

TestOperation:DiffPerc

If DiffPerc  < 100 
    "Difference is there and take value of DiffPerc to calculate average"
else 
    "Dont take that record"


TestOperationDifference:DiffPerc,DiffRec
If DiffPerc  < 100 
    "Difference is there and take value of DiffPerc and DiffRec to calculate average"
else 
    "Dont take that record"




finalAverage=(
                   Average(TestOperation.DiffPerc) 
                    + Average(TestOperationDifference.DiffPerc)
                    + Average(TestOperationDifference.DiffRec)
              )/3

输出如下:

[0]=Mobile 
    Electronics
    FinalAverage=30.00
[1]=Shoes 
    Sports
    FinalAverage=70.00
.
.
.

我的代码:

public class Category
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public Nullable<int> ParentId { get; set; }
            public virtual ICollection<Variants> Variants { 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; }
        }

        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 decimal DiffPerc { 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 decimal DiffPerc { get; set; }
            public decimal DiffRec { get; set; }
            public virtual SubVariants SubVariants { get; set; }
            public virtual SubVariants SubVariants1 { get; set; }
            public virtual Test Test { get; set; }
        }

我的查询:

 var query = from cat in context.Category
             join v in context.Variants on cat.Id equals v.CategoryId
             join sv in context.SubVariants on v.Id equals sv.VariantId
             join to in context.TestOperation on sv.Id equals to.SourceSubVariantId
join tod in context.TestOperationDifference on sv.Id equals tod.SourceSubVariantId
             where 
              (to.DiffPerc < 100) 
                 ||
              (tod.DiffPerc < 100 )
               group cat by new {catid = cat.Id} into grp 
              select new 
              {
                subcategoryname=grp. //not getting property here                 
                ParentCategoryName=grp.
                FinalAverage=    
              }

但是在上面的查询中,当我尝试访问子类别名称时,我无法访问它。

演示小提琴

基本查询应如下所示

 var query = (from cat in category
                         join v in variants on cat.Id equals v.CategoryId
                         join sv in subVariants on v.Id equals sv.VariantId
                         into grp
                         select new { id = cat.Id, subvariant = v.SubVariants, name = cat.Name, type = v.Type})
                         .GroupBy(x => new {id = x.id, subvariant = x.subvariant});

您需要将testOperations和testOperationDifferences应用于查询。

那是因为grp是集合的IGrouping实例。 正如您可能已经猜到的那样,您需要访问集合的某些元素以获取CategoryName属性,或者您可以通过NameId对您的Category集合进行分组(如果在您的情况下有意义),那么您可以访问它使用grp.Key.Name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM