【问题标题】:linq join throw an operation timeout error?linq join 抛出操作超时错误?
【发布时间】:2016-03-17 09:03:49
【问题描述】:

由于复杂的数据关系,我有多个表连接的查询, 为了优化查询,所以我设置了 linq ObjectTrackingEnabled 为 false,然后构建查询并加入我自己。

var tempapp = (
    from app in CTX.user_applications
    join user in CTX.user_lists on app.user_id equals user.user_id
    join pst in CTX.postings on app.posting_id equals pst.posting_id
    join job in CTX.job_ms on pst.job_id equals job.job_id
    join loc in CTX.job_location_ms on pst.location_id equals loc.job_location_id
    join jobcat in CTX.job_category_ms on pst.job_cat_id equals jobcat.job_category_id
    join offcat in CTX.office_category_ms on pst.office_cat_id equals offcat.office_category_id

    from appstat in CTX.app_status_ms.Where(stat => stat.app_status_id == app.app_status_id).DefaultIfEmpty() 

    from address in CTX.user_addresses.Where(addr => addr.user_id == user.user_id && addr.address_type == 0).DefaultIfEmpty()

    from state in CTX.state_ms.Where(st => st.state_id == address.state_id).DefaultIfEmpty()

    from edu in CTX.user_edus.Where(ed => ed.user_id == user.user_id).DefaultIfEmpty()

    select new  CustomObj
                {
                    application_id = app.user_app_id,
                    job_cat_id = jobcat.job_category_id,
                    job_cat_desc = jobcat.PSF_Desc,
                    off_cat_id = offcat.office_category_id,
                    off_cat_desc = offcat.PSF_Desc,
                    loc_id = loc.job_location_id,
                    loc_desc = loc.PSF_Desc,
                    job_id = job.job_id,
                    job_desc = job.PSF_Desc,
                    state_id = state.state_id,
                    state_desc = state.state_desc,

                    edu_lvl_id = edu.edu_lvl_id,
                    applied_date = app.applied_date,
                    manager_id = app.manager_id, 
                    gender_id = user.gender_id,
                    birthdate = user.birthday,
                    status_web = app.status_web,
                    status_psf = appstat.status_web,
                    user_id = user.user_id,
                    app_status_id  = app.app_status_id,
                    online_test_id = app.online_test_id

                }
    ).ToList();

我不知道为什么,但是这个查询抛出了超时错误:

超时。在完成之前超时时间已过 操作或服务器没有响应。

有人可以指点我来解决这个问题吗?

【问题讨论】:

  • 如果请求超过 30 秒,Linq 将停止。设置DataContext.CommandTimeout 来避免这种情况
  • @Nitro.de 谢谢,我会试一试。

标签: c# asp.net sql-server linq


【解决方案1】:

原因是您的查询只需要很长时间才能执行。就像 nitro.de 已经评论过的那样,您可以将上下文中的 CommandTimeout 设置为更高的值。

由于语句末尾的ToList()LINQ 正在一次查询所有数据。也许您可以省略 ToList 并使用 IEnumerable,以便稍后评估查询并在需要时加载每条记录。

另一个(也许更好)的选择是在您的数据库中创建一个视图,以连接 SQL 中的数据并从代码中查询该视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多