var qGroup = from dispatch in _dispatchRepository.GetAll() group dispatch by dispatch.DeviceId into g select g.Key + "_" + g.Max(d => d.BeginTime); var query = from dispatch in _dispatchRepository.GetAll() where qGroup.Contains(dispatch.DeviceId + "_" + dispatch.BeginTime) select dispatch;