I have following records
User
---------------
UserID | Name
---------------
1 | Johan
2 | Rick
---------------
UserDetail
----------------------------------
UserDetailID | UserID | Percentage
----------------------------------
1 | 1 | 10
2 | 1 | 30
3 | 2 | 50
4 | 2 | 10
----------------------------------
Basically I want to SUM percentage per UserID and Order by the largest Total Percentage.
In TSQL :
SELECT
User.UserID
FROM
User
LEFT JOIN
(
SELECT
SUM(Percentage) [Total],
UserID
FROM
UserDetail
GROUP BY
UserID
) qry ON
User.UserID = qry.UserID
ORDER BY
qry.Total DESC
My current code :
var orderedList = user.SelectMany(s => s.UserDetails).Sum(s => s.Percentage); // <-- I have no idea what I'm doing
It sounds like you don't want to use SelectMany
at all - you only want one row per user. I suspect you want:
var orderedList = users.OrderByDescending(user => user.UserDetails.Sum(d => d.Percentage));
See more on this question at Stackoverflow