【问题标题】:How to write fast subquery in where condition in linq如何在linq中的where条件下编写快速子查询
【发布时间】:2014-11-04 13:38:56
【问题描述】:

我尝试在 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 编写代码?

谢谢。

【问题讨论】:

  • 谢谢,但我的问题不是 nolock 表达式。我的问题是 where 条件下的子查询。 Linq 表达式在 sql 查询中变得混乱,并且出现错误超时。我想在 sql server 配置文件中查看我的 linq 语法,就像我的问题 sql 一样。

标签: c# sql sql-server linq subquery


【解决方案1】:

试试:

    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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多