简单的SQL到Linq查询,具有分组和汇总功能 [英] Simple sql to Linq query with group by and aggregate functions
本文介绍了简单的SQL到Linq查询,具有分组和汇总功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在与linq尝试学习语法,但我不知道如何执行以下简单查询
I'm fighting with linq trying to learn the syntax and I can't figure out how to do the following simple query
SELECT DISTINCT
user.firstname,
user.lastname,
COUNT(invoice.amount),
SUM(invoice.amount)
FROM
company_user
INNER JOIN
user
ON
company_user.user_id = user.user_id
INNER JOIN
invoice
ON
invoice.user_id= invoice.user_id
WHERE
company_user.company_id = 1
GROUP BY
user.firstname,
user.lastname,
GO
将其转换为linq的任何帮助都会很棒.
Any help turning this into linq would be great.
推荐答案
您要查询的内容应与此非常接近:
The query you're after should be pretty close to this:
var query =
from cu in company_user
where cu.company_id == 1
join u in user on cu.user_id equals u.user_id
join i in invoice on u.user_id equals i.user_id
group i.amount by new
{
u.firstname,
u.lastname,
} into gs
select new
{
firstname = gs.Key.firstname,
lastname = gs.Key.lastname,
count = gs.Count(),
sum = gs.Sum(),
};
享受!
这篇关于简单的SQL到Linq查询,具有分组和汇总功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文