left join 一个子查询,这种sql 怎么用linq实现

antony1029 2016-03-31 02:26:36
select *  
FROM Customer c
left join(
Select o.CustomerId ,sum(o.OrderTotalAmount) as OrderTotalAmount from [order] o
group by o.CustomerId
)ot
on c.id=ot.CustomerId

这种sql 用linq怎么实现
...全文
376 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
antony1029 2016-04-01
  • 打赏
  • 举报
回复
引用 3 楼 q107770540 的回复:
var query = from c in db.Customer
            join ot in db.order.GroupBy(o=>o.CustomerId).Select(o=>new{CustomerId=o.Key,OrderTotalAmount=o.Sum(x=>x.OrderTotalAmount) })
			on c.id equals ot.CustomerId into lg
			from c in lg.DefaultIfEmpty()
			select c;
感谢,非常正确。比我的执行效率要高。
q107770540 2016-03-31
  • 打赏
  • 举报
回复
var query = from c in db.Customer
            join ot in db.order.GroupBy(o=>o.CustomerId).Select(o=>new{CustomerId=o.Key,OrderTotalAmount=o.Sum(x=>x.OrderTotalAmount) })
			on c.id equals ot.CustomerId into lg
			from c in lg.DefaultIfEmpty()
			select c;
antony1029 2016-03-31
  • 打赏
  • 举报
回复
还有其他的写法吗??
antony1029 2016-03-31
  • 打赏
  • 举报
回复
            var q =
from c in customer
join o in _orde on c.Id
equals o.CustomerId into orders
select new
{
c.Username,
OrderTotal = orders.Sum(i=>i.OrderTotal)

};

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧