无法通过组访问领域和多个连接使用LINQ [英] Not able to access fields with group by and multiple joins with linq

查看:141
本文介绍了无法通过组访问领域和多个连接使用LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只列出那些类别平均已在他们的记录差

I want to list only those categories with average which have difference in their records.

在2表这种差异存在: TestOperation,TestOperationDifference

This difference exist in 2 tables:TestOperation,TestOperationDifference

我要计算的低于3场平均水平:

I want to calculate average of below 3 fields :

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
.
.
.



我的代码:

My Code:

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; }
        }



我的查询:

My Query:

 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=    
              }



但在这里上面查询时,我试图。访问子类别名称,然后我
我不能够访问它。

But here in above query when i am trying to access subcategory name then i am not able to access it.

示范小提琴:的代码小提琴

任何人可以帮助我?

推荐答案

中的基本查询应该是这样的。

The basic query should look like this

 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到查询。

The you need to apply the testOperations and testOperationDifferences to the query.

这篇关于无法通过组访问领域和多个连接使用LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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