linq平均计数语法

时间:2012-04-05 12:29:39

标签: linq

我有一个列表(pk = Id)。 该表与PracticeProfileId有一个实践文件的FK (即1个练习简介有很多列表)

我想回复每个练习资料的AVERAGE数量

int count;
count = (from l in myentity.Listings
        group l by l.PracticeProfileId into g
        select g.Count()).Average();

我认为我已成功通过practiceprofile对列表进行分组,但我不确定如何获得总平均列表数量

感谢

2 个答案:

答案 0 :(得分:0)

您应该使用分组依据,请尝试以下示例:

var categories = 
    from p in products 
    group p by p.Category into g 
    select new { Category = g.Key, AveragePrice = g.Average(p => p.UnitPrice) }; 

答案 1 :(得分:0)

我想在每个类别中包含产品数量,以及平均单价。我无法找到正确的语法。

var categories =
from p in products
group p by p.Category 
into g      
select new { Category = g.Key, 
             ProdsInCat = g.Count(), 
             AveragePrice = g.Average(p => p.UnitPrice) }; 

编辑添加:

这是正确的语法。我发现了我的错误;我将ProdsInCat定义为String而不是Int