我希望能够以某种方式打破了下面的代码:重构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 does not recognized the method'exception。
可能重复LINQ IQueryable表达式删除重复部分的查询](http://stackoverflow.com/questions/769351/refactoring-linq-iqueryable-expression-to-remove-duplicated-portions-of-queries) – Merritt 2011-05-12 18:49:32