1
谁能帮我写等价的SQL选择组值的LINQ C#如何使用LINQ to SQL的
select
COUNT(ot.UserId) as orderCount, SUM(ot.TotalAmount) as TotalAmount,
MAX(u.UserName) as UserName, MAX(c.name) as ClientName
from OrderTemplate ot
join User u
on u.userid = ot.UserId
join Client c
on u.ClientID = c.clientid
group by ot.UserId
我所做的是,
from ot in dbContext.OrderTemplates
join user in dbContext.Users on ot.UserId equals user.UserID
join client in dbContext.Clients on user.ClientID equals client.ClientID
group ot by ot.UserId into g
select new
{
//UserName = need to pick this from user table
//ClientName = need to pick this from client table
OrderCount = g.Count(),
TotalAmount = g.Sum(x=> x.TotalAmount)
};
我可以” t根据sql选择值。
“用户名”的值是什么? “Max(u.Username)”实际上是什么意思?那些是相同的用户名?是一些'null'的值,然后你做'max'来得到那个不是? –
是的,每个组的用户名都是一样的,所以我只需要选择一个。 –
另外在你的sql中,你正在用'UserId'打断,但在'orderId'的linq中 –