gpt4 book ai didi

c# - linq to sql Distinct 和 orderby

转载 作者:IT王子 更新时间:2023-10-29 04:11:12 26 4
gpt4 key购买 nike

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(如下)并不像想象的那么简单

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

提前致谢

最佳答案

不知道有没有用,不过你可以试试这样的方法;

var result = (from o in table1
join t in table2 on o.ProgramID equals t.ProgramID
orderby t.Program
select new { o.ProgramID, t.Program }).Distinct();

关于c# - linq to sql Distinct 和 orderby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4318909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com