使用 let 变量和子查询重构 LINQ to Entities 查询
我希望能够以某种方式分解以下代码:
return from e in _context.Employees
let HasWatchedAllVideos =
(
from ev in _context.EmployeeVideos
where ev.EmployeeId == e.Id && ev.EndTime.HasValue
select ev.Id
).Count() == _context.Videos.Count()
let EndTime = HasWatchedAllVideos ?
(
from ev in _context.EmployeeVideos
where ev.EmployeeId == e.Id
select ev.EndTime
).Max() : null
let StartTime =
(
from ev in _context.EmployeeVideos
where ev.EmployeeId == e.Id
select ev.StartTime
).Min()
select new EmployeeListItem
{
Id = e.Id,
FirstName = e.FirstName,
LastName = e.LastName,
Company = e.Company,
HasWatchedAllVideos = HasWatchedAllVideos,
StartTime = StartTime,
EndTime = EndTime
};
例如,我正在寻找一种方法将:
let HasWatchedAllVideos =
(
from ev in _context.EmployeeVideos
where ev.EmployeeId == e.Id && ev.EndTime.HasValue
select ev.Id
).Count() == _context.Videos.Count()
分解为一个单独的方法以实现可重用性,但我无法确切地弄清楚如何执行此操作。我尝试过:
private bool HasWatchedAllVideos(int employeeId)
{
return (from ev in _context.EmployeeVideos
where ev.EmployeeId == employeeId && ev.EndTime.HasValue
select ev.Id
).Count() == _context.Videos.Count();
}
这给了我我最喜欢的“LINQ to Entities 无法识别该方法”异常。
I want to be able to somehow break up the following code:
return from e in _context.Employees
let HasWatchedAllVideos =
(
from ev in _context.EmployeeVideos
where ev.EmployeeId == e.Id && ev.EndTime.HasValue
select ev.Id
).Count() == _context.Videos.Count()
let EndTime = HasWatchedAllVideos ?
(
from ev in _context.EmployeeVideos
where ev.EmployeeId == e.Id
select ev.EndTime
).Max() : null
let StartTime =
(
from ev in _context.EmployeeVideos
where ev.EmployeeId == e.Id
select ev.StartTime
).Min()
select new EmployeeListItem
{
Id = e.Id,
FirstName = e.FirstName,
LastName = e.LastName,
Company = e.Company,
HasWatchedAllVideos = HasWatchedAllVideos,
StartTime = StartTime,
EndTime = EndTime
};
For example, I am looking for a way to factor out:
let HasWatchedAllVideos =
(
from ev in _context.EmployeeVideos
where ev.EmployeeId == e.Id && ev.EndTime.HasValue
select ev.Id
).Count() == _context.Videos.Count()
into a separate method for reusability purposes, yet I can't figure out exactly how to go about doing this. I have tried:
private bool HasWatchedAllVideos(int employeeId)
{
return (from ev in _context.EmployeeVideos
where ev.EmployeeId == employeeId && ev.EndTime.HasValue
select ev.Id
).Count() == _context.Videos.Count();
}
Which gives me my old favorite, 'LINQ to Entities does not recognize the method' exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题可能不会得到很多关注,所以我发布了一个相关问题,帮助我尝试找到更好的解决方案:
重构 LINQ IQueryable 表达式以删除查询的重复部分
以下是我的解决方案的特定变体的代码:
--更新 5/13/2011 -
上面的示例执行内部联接,并且对于您想要包含所有员工的情况不起作用,即使 EmployeeVideoAggregatesView() 没有返回结果,因此为了允许左外部联接,我必须调整代码一点:
This question probably won't get a lot of action, so I am posting a related question that has helped me try to find a better solution:
refactoring LINQ IQueryable expression to remove duplicated portions of queries
Here's the code for my particular variation of a solution:
-- UPDATE 5/13/2011 --
The example above performs an inner join and will not work for instances where you want to include all employees even if EmployeeVideoAggregatesView() returns no results, so to allow for left outer joining, I had to tweak the code a bit: