将SQL转换为LINQ和Lambda

时间:2014-05-30 11:11:15

标签: c# sql linq

请有人能帮我将以下SQL查询转换为LINQ

select p.Description,SUM(s.TotalArea) as TotalArea from Stands s
inner join ContractProducts cp on s.Id = cp.StandId 
inner join Products p on cp.ProductId = p.Id
where s.EventId = 1
group by p.Description

提前致谢

1 个答案:

答案 0 :(得分:1)

也许是这样的:

var result= (
        from s in db.Stands
        join cp in db.ContractProducts
            on s.Id equals cp.StandId
        join p in db.Products
            on cp.ProductId equals p.Id
        where s.EventId == 1
        group p by p.Description into g
        select new
        {
            Description=g.Key,
            TotalArea = g.Sum (x =>x.TotalArea)
        }
    ).ToList();

其中db是linqdatacontext