我想仅列出那些平均值与记录不同的类别。
这种差异存在于两个表中: 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=
}
但是在上面的查询中,当我尝试访问子类别名称时,我 我无法访问它。
答案 0 :(得分:3)
基本查询应如下所示
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应用于查询。
答案 1 :(得分:2)
之所以发生这种情况,是因为grp
是集合IGrouping
的实例。正如您可能已经猜到的那样,您需要访问集合的某些元素才能获得Name
的{{1}}属性,或者您可以将Category
的集合分组到Category
并且Name
(如果在您的情况下有意义),因此您可以使用Id