linq平均计数语法

linq平均计数语法,linq,Linq,我有一个列表(pk=Id)。 该表具有PracticeProfile by PracticeProfile ID指向PracticeProfile的FK (即1个机构档案有许多清单) 我想返回每个机构配置文件的平均列表数 int count; count = (from l in myentity.Listings group l by l.PracticeProfileId into g select g.Count()).Average(); 我想我已经成功

我有一个列表(pk=Id)。 该表具有PracticeProfile by PracticeProfile ID指向PracticeProfile的FK (即1个机构档案有许多清单)

我想返回每个机构配置文件的平均列表数

int count;
count = (from l in myentity.Listings
        group l by l.PracticeProfileId into g
        select g.Count()).Average();
我想我已经成功地按practiceprofile对列表进行了分组,但我不确定如何获得列表的总平均值


谢谢

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

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

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

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

我认为您的代码是正确的。如果没有,您能否提供样本输入和预期结果?