如何在linq中的where条件中编写快速子查询

时间:2014-11-04 13:38:57

标签: c# sql sql-server linq subquery

我尝试在linq中编写子查询where条件。但是,当我在sql server profiler中查看我的sql语法时,我的查询看起来很复杂且很长,并且在服务器中会出现超时错误。

我想写一个如下的查询。

SELECT 
T1.Id,
T2.Id,
DATEADD(HOUR, 
    ISNULL(
    (
        SELECT 
            MAX(T3.MaxHour) AS [MaxHour]
        FROM TABLE3 T3 WITH(NOLOCK)
        WHERE 
            T1.CategoryId=T3.CategoryId
    ),
    0),T1.EndDate
    ),
T1.StartDate 
FROM   
    TABLE1 T1 WITH(NOLOCK)    
LEFT OUTER JOIN TABLE2 T2 WITH(NOLOCK)  ON T1.Id=T2.Id  
WHERE
    DATEADD(HOUR, 
    ISNULL(
    (
        SELECT 
            MAX(T3.MaxHour) AS [MaxHour]
        FROM TABLE3 T3 WITH(NOLOCK)
        WHERE 
            T1.CategoryId=T3.CategoryId
    ),
    0),T1.EndDate
    ) 
    > '2014-11-05 00:00:00'
AND
    T1.StartDate < '2014-11-05 06:00:00'

我写了这样的linq表达式。

        var actualData = from T1 in _context.TABLE1
                         join T2 in _context.TABLE2 on T1.Id equals T2.Id into data
                         from x in data.DefaultIfEmpty()
                         where
                             EntityFunctions.AddHours(T1.EndDate,
                             (
                                (int?)((from T3 in _context.TABLE3
                                        where
                                             T3.CategoryId == T1.CategoryId
                                        select
                                            T3).Max(row => row.MaxHour))
                             ) ?? 0
                             ) >= '2014-11-05 00:00:00'
                             && T1.StartDate < '2014-11-05 06:00:00'

                         select
                             new 
                             {
                                 Id1 = T1.Id,
                                 Id2 = T2.Id,
                                 StartDate = T1.StartDate,
                                 EndDate =  EntityFunctions.AddHours(T1.EndDate,
                                    (
                                        (int?)((from T3 in _context.TABLE3
                                        where
                                             T3.CategoryId == T1.CategoryId
                                        select
                                            T3).Max(row => row.MaxHour))
                                    ) ?? 0
                                )

                             };

但是在我想看的sql中似乎没有。

怎么能用linq写?

感谢。

1 个答案:

答案 0 :(得分:0)

尝试:

    var actualData = from T1 in _context.TABLE1
                     join T2 in _context.TABLE2 on T1.Id equals T2.Id into data
                     from x in data.DefaultIfEmpty()

                     let endate = EntityFunctions.AddHours(T1.EndDate,
                         (
                            (
                                from T3 in _context.TABLE3
                                where T3.CategoryId == T1.CategoryId
                                select T3).Max(row => row.MaxHour)
                            )
                         ) ?? 0
                     )

                     where
                         enddate >= '2014-11-05 00:00:00'
                         && T1.StartDate < '2014-11-05 06:00:00'

                     select
                         new 
                         {
                             Id1 = T1.Id,
                             Id2 = T2.Id,
                             StartDate = T1.StartDate,
                             EndDate = enddate
                         };

我怀疑你可以将演员表放到(int?),因为max(int)默认返回int?