我继承了下面的LINQ查询,感觉可以重构查询来提高效率。该查询目前需要大约 6-8 秒的处理时间才能在应用程序的前端向用户返回一条记录。LINQ 不是我的强项,所以任何帮助将不胜感激。查询最终应该生成一个不同的 CA_TASK_VW 对象列表,这些对象与从 CA_OBJECT、CA_PEOPLE 和 CA_CONTRACTOR 表中获得的不同 CA_OBJECT_ID 的列表相关联。var data = (from a in _db.CA_TASK_VW where a.TASK_TYPE == "INSPECTION" && a.TASK_AVAILABLE_FLAG == "Y" && a.TARGET_END_DATE == null select a).AsQueryable();data = data.Join(_db.CA_OBJECT.Where(o => o.ENTERED_BY == _userId), o => o.CA_OBJECT_ID, p => p.CA_OBJECT_ID, (t, p) => t) .Union(data.Join(_db.CA_PEOPLE.Where(p => p.EMAIL == _email), t => t.CA_OBJECT_ID, p => p.CA_OBJECT_ID, (t, p) => t)) .Union(data.Join(_db.CA_CONTRACTOR.Where(c => c.CONTRACTOR.EMAIL == _email), t => t.CA_OBJECT_ID, c => c.CA_OBJECT_ID, (t, c) => t));
2 回答
慕桂英4014372
TA贡献1871条经验 获得超13个赞
代码好像是用Join/Union来执行基本的 where 列表上的一个 where 谓词,一CA_TASK_VW步步过滤到最后的结果,那么直接指定 where 条件会怎么样呢?
var data = from a in _db.CA_TASK_VW
where a.TASK_TYPE == "INSPECTION" && a.TASK_AVAILABLE_FLAG == "Y" && a.TARGET_END_DATE == null
select a;
data = data.Where(t => _db.CA_OBJECT.Where(o => o.ENTERED_BY == _userId).Select(o => o.CA_OBJECT_ID).Contains(t.CA_OBJECT_ID) ||
_db.CA_PEOPLE.Where(p => p.EMAIL == _email).Select(p => p.CA_OBJECT_ID).Contains(t.CA_OBJECT_ID) ||
_db.CA_CONTRACTOR.Where(c => c.CONTRACTOR.EMAIL == _email).Select(c => c.CA_OBJECT_ID).Contains(t.CA_OBJECT_ID));
- 2 回答
- 0 关注
- 74 浏览
添加回答
举报
0/150
提交
取消