【问题标题】:Convert SQL into LINQ and Lambda将 SQL 转换为 LINQ 和 Lambda
【发布时间】:2014-05-30 11:11:15
【问题描述】:

请有人能帮我将以下 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

提前致谢

【问题讨论】:

  • 转换为 lambda 是什么意思? lambda 与 sql 和 linq 无关,它只是一个匿名方法。
  • 请发布您到目前为止所尝试的内容。这个问题看起来有点懒。

标签: c# sql linq


【解决方案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

【讨论】:

  • 感谢 Arion,只需按 s 分组即可到达 TotalArea
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 2018-09-23
  • 2021-05-30
  • 2020-10-23
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多