As in the previous question , I have the following LINQ expression.
Events.Where(Function(e) e.EventDate >= Date.Today) _
.OrderByDescending(Function(e) (((e.EventVotes.Sum(Function(s) s.Vote)) * 2) + (e.Comments.Count))) _
.Skip(0) _
.Take(5)
What will be converted to the next SQL
DECLARE @p0 DateTime2 = '2011-01-17 00:00:00.0000000'
DECLARE @p1 Int = 2
DECLARE @p2 Int = 0
DECLARE @p3 Int = 5
SELECT [t3].[ID], [t3].[UserID], [t3].[RegionID], [t3].[LocationID], [t3].[Title], [t3].[Description], [t3].[EventDate], [t3].[URL], [t3].[Phone], [t3].[TicketPriceLow], [t3].[TicketPriceHigh], [t3].[DatePosted], [t3].[isHighlighted]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY (((
SELECT SUM([t1].[Vote])
FROM [dbo].[EventVotes] AS [t1]
WHERE [t1].[EventID] = [t0].[ID]
)) * @p1) + ((
SELECT COUNT(*)
FROM [dbo].[Comments] AS [t2]
WHERE [t2].[EventID] = [t0].[ID]
)) DESC) AS [ROW_NUMBER], [t0].[ID], [t0].[UserID], [t0].[RegionID], [t0].[LocationID], [t0].[Title], [t0].[Description], [t0].[EventDate], [t0].[URL], [t0].[Phone], [t0].[TicketPriceLow], [t0].[TicketPriceHigh], [t0].[DatePosted], [t0].[isHighlighted]
FROM [dbo].[Events] AS [t0]
WHERE [t0].[EventDate] >= @p0
) AS [t3]
WHERE [t3].[ROW_NUMBER] BETWEEN @p2 + 1 AND @p2 + @p3
ORDER BY [t3].[ROW_NUMBER]
My problem now is when it comes to ordering, when some events have no votes or comments.
This is what the EventVotes table looks like (whole)
| UserID | EventID | Vote |
| 1 | 51 | 1 |
| 1 | 52 | 1 |
| 2 | 52 | 1 |
| 1 | 53 | 1 |
| 2 | 53 | -1 |
| 3 | 53 | -1 |
The comment table is completely empty, since we just do Countit, we can assume that everything returns Null.
Now, when I run the query above, the order of the result is as follows
52
51
53
1
2
3
When should it be
52
51
1
2
3
53
53 "-1", 1, 2 3 "0"
- , Linq , ?

EDIT:
, LinqPad , .
Events.OrderByDescending(Function(e) (((e.EventVotes.Sum(Function(s) s.Vote)) * 2) + (e.Comments.Count)))
, orderby (51, 52, 53), . "" Lambda.