return context.Table1s.GroupBy(i => i.Policy)
.Select(g => g.First())
.Orderby(i => i.Policy)
.Skip(endingRecord).Take(page)
.ToList();
这会生成这样的 SQL(来自 LinqPad 的 Linq to SQL 示例):
SELECT [t4].[test], [t4].[Name], [t4].[Policy], [t4].[Amount], [t4].[Date]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t3].[Policy]) AS [ROW_NUMBER], [t3].[test], [t3].[Name], [t3].[Policy], [t3].[Amount], [t3].[Date]
FROM (
SELECT [t0].[Policy]
FROM Table1s AS [t0]
GROUP BY [t0].[Policy]
) AS [t1]
OUTER APPLY (
SELECT TOP (1) 1 AS [test], [t2].[Name], [t2].[Policy], [t2].[Amount], [t2].[Date]
FROM Table1s AS [t2]
WHERE (([t1].[Policy] IS NULL) AND ([t2].[Policy] IS NULL)) OR (([t1].[Policy] IS NOT NULL) AND ([t2].[Policy] IS NOT NULL) AND ([t1].[Policy] = [t2].[Policy]))
) AS [t3]
) AS [t4]
WHERE [t4].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
ORDER BY [t4].[ROW_NUMBER]