由于数据关系复杂,我有多个表连接的查询, 为了优化查询,我设置了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();
我不知道为什么,但是这个查询会抛出超时错误:
超时已过期。完成之前已经过了超时时间 操作或服务器没有响应。
有人能指出我,解决这个问题吗?
答案 0 :(得分:1)
原因是您的查询需要很长时间才能执行。就像nitro.de已经评论过的那样,您可以将上下文中的CommandTimeout设置为更高的值。
由于语句末尾的<li>
,ToList()
一次查询所有数据。也许您可以省略ToList并使用LINQ
,因此稍后会对查询进行评估,并在需要时加载每条记录。
另一个(可能更好)选项是在数据库中创建一个视图,以便在SQL中连接数据并从代码中查询该视图。