日期:2014-05-20 浏览次数:21040 次
from c in Customers join p in Purchases on c.ID equals p.CustomerID group p.Price by p.Date.Year into salesByYear select new { Year=salesByYear.Key, TotalValue =salesByYear.Sum() } 最后呈现两个内容,一个是年份,一个是所有客户消费的金额总和。 对应的SQL: SELECT SUM([t2].[Price]) AS [TotalValue], [t2].[value] AS [Year] FROM ( SELECT DATEPART(Year, [t1].[Date]) AS [value], [t1].[Price] FROM [Customer] AS [t0] INNER JOIN [Purchase] AS [t1] ON ([t0].[ID]) = [t1].[CustomerID] ) AS [t2] GROUP BY [t2].[value]
------解决方案--------------------
(from c1 in c join d1 in d on c.userid equals d.userid group d1.userid by { c.name, c.userid } into grouptable select new { names=grouptable.Key.name, userid=grouptable.Key.userid, countuserid=grouptable.Sum() }).Take(5)