简体   繁体   English

SQL到LINQ-使用等于或大于等于的值从同一表左联接

[英]SQL to LINQ - left join from same table using values equal to and greater than

I have the following SQL query which I am trying to convert to LINQ. 我尝试将以下SQL查询转换为LINQ。

SELECT   t1.* 
FROM   table1 t1 
LEFT JOIN   table1 t2 
ON  (t1.MusicId = t2.MusicId AND t1.MusicDetailId > t2.MusicDetailId) 
WHERE t2.MusicDetailId IS NULL and t1.SingerId = 2
ORDER BY t1.MusicId 

I have tried the following but I am not getting the correct data back. 我已经尝试了以下方法,但是没有得到正确的数据。

var query =
    from   t1 in table1
    from t2 in table1
    where t1.MusicId == t2.MusicId && t1.MusicDetailId > t2.MusicDetailId 
    where t1.SingerId == 2 && t2.MusicDetailId == null
    orderby t1.MusicId 
    select t1;

Is anyone able to help to get this SQL query converted to LINQ correctly? 有谁能帮助您将此SQL查询正确转换为LINQ?

   var query = from t1 in table1.Where(X=> X.SingerId == 2)
                    join t2 in table1.Where(X=>X.MusicDetailId ==null) on t1.MusicId  equals t2.MusicId 
where  t1.MusicDetailId > t2.MusicDetailId
                    select t1 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM