无法使用group-by访问字段,也无法使用linq访问多个联接
本文关键字:访问 字段 group-by linq | 更新日期: 2023-09-27 17:54:13
我只想列出那些记录中有差异的具有平均值的类别。
这种差异存在于两个表中: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=
}
但在上面的查询中,当我试图访问子类别名称时我无法访问它。
演示Fiddle
基本查询应该看起来像这个
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
的实例。正如您可能已经猜到的那样,您需要访问集合的某个元素来获得Category
的Name
属性,或者您可以通过Name
和Id
对Category
集合进行分组(如果在您的情况下有意义的话(,因此您可以使用grp.Key.Name
来访问它