【问题标题】:select average of a column from joined tables in linq从 linq 中的联接表中选择列的平均值
【发布时间】:2015-06-07 05:52:10
【问题描述】:

我有这个 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格式写,我写了如下但我不知道如何使用avergae函数来传递.Duration,它不存在p.Duration的平均函数!

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};

【问题讨论】:

标签: linq join average


【解决方案1】:

试试这个:

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)};

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 2020-01-03
    • 2014-12-10
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多