我希望能夠以某種方式打破了下面的代碼:重構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