1

我有一个列表(pk = Id)。该表有一个由 PracticeProfileId 到 practiceprofile 的 FK(即 1 个练习配置文件有许多列表)

我想返回每个练习资料的平均列表数量

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

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

谢谢

4

2 回答 2

0

您应该使用 Group By,请尝试以下示例:

var categories = 
    from p in products 
    group p by p.Category into g 
    select new { Category = g.Key, AveragePrice = g.Average(p => p.UnitPrice) }; 
于 2012-04-05T12:38:58.743 回答
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) }; 

编辑添加:

这是正确的语法。我发现了我的错误;我定义ProdsInCatString而不是Int.

于 2012-08-17T17:35:09.570 回答