【问题标题】:SQL to LINQ - left join from same table using values equal to and greater thanSQL to LINQ - 使用等于和大于的值从同一个表左连接
【发布时间】:2016-01-06 05:00:02
【问题描述】:

我有以下 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 

我尝试了以下方法,但没有得到正确的数据。

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;

是否有人能够帮助将此 SQL 查询正确转换为 LINQ?

【问题讨论】:

    标签: c# sql linq join left-join


    【解决方案1】:
       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 ;
    

    【讨论】:

    • 非常感谢您,我可以解决这个问题以到达我想要的地方。一个小错误虽然在第二个 where 子句中缺少 lamda x =>.
    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多