LINQ和Entity Framework中的Join操作

19

在SQL中,为了使用DISTINCT语句,我使用了join来实现,代码如下:

select distinct 
    col1 
from 
    table1 a 
inner join 
    table2 b on a.code = b.vcode

如何在Entity Framework中使用LINQ实现相同的功能?

请给我建议。

2个回答

43

您也可以使用方法语法:

var query = table1.Join(table2,
                        a => a.code,
                        b => b.vcode,
                        (a,b) => a.col1)
                   .Distinct();

感谢您提供另一种解决方案。 - user1893874
这样会好得多。 - deathrace

24
var result = (from a in table1
              join b in table2 on a.code equals b.vcode
              select a.col1).Distinct();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接