var result = table1.Join(table2, o => o.ProgramID, t => t.ProgramID, (o, t) => new { o.ProgramID, t.Program })
.OrderBy(t => t.Program)
.Distinct();
上述LINQ語句實際返回正確的結果,但他的SQL生成(下同)不是那麼簡單,因爲它可以LINQ to SQL的獨特和排序依據
SELECT [t2].[ProgramID], [t2].[Program]
FROM (
SELECT DISTINCT [t0].[ProgramID], [t1].[Program]
FROM [table1] AS [t0]
INNER JOIN [table2] AS [t1] ON [t0].[ProgramID] = [t1].[ProgramID]
) AS [t2]
ORDER BY [t2].[Program]
我想到,SQL下面是更清潔,但我不確定linq聲明來實現它。
select distinct
o.ProgramID,
t.Program
from
table1 0
inner join table2 t on t.ProgramID = o.ProgramID
order by t.Program
在此先感謝
您是否嘗試過調用方法調用的順序?即交換orderby和不同? – marr75 2010-11-30 21:41:20
真棒,交換了他們,並工作了一種享受。 – Ian 2010-12-01 02:09:55