在linq中选择连接表中列的平均值



我有这个SQL代码:

select  AVG(passing.Duration), passing.pId, p.name, zn.title 
from passing inner join zn on passing.zId = zn.zId 
inner join p on passing.pId = p.pId 
group by  passing.pId, zn.title, p.name

和我想写它在linq格式,我已经写了如下,但我不知道如何使用平均函数传递。持续时间,它不存在p的平均函数持续时间!

var pass = from p in db.passings
           join z in db.zns on p.zId equals z.zId
           join pr in db.ps on p.pId equals pr.pId
           select new {p.Duration, p.pId, pr.name, z.title};

试试这个:

var pass = from p in db.passings
       join z in db.zns on p.zId equals z.zId
       join pr in db.ps on p.pId equals pr.pId
       select new {p.Duration, p.pId, pr.name, z.title} into temp
       from t in temp
       group t by new{t.pId, t.name, t.title} into gr
       select new{gr.Key.pId, 
                  gr.Key.name, 
                  gr.Key.title, 
                  duration = gr.Average(c=>c.Duration)};

相关内容

  • 没有找到相关文章

最新更新