简体   繁体   English

将Iqueryable与Generic Repository一起使用时找不到Records

[英]Using Iqueryable with Generic Repository doesn't find Records

I have a known working Repository. 我有一个已知的工作存储库。 Using structuremap as IOC. 使用structuremap作为IOC。

But I can't do any Iqueryable searches using the following: 但我不能使用以下内容进行任何Iqueryable搜索:

 private IRepository<Employee> _employeeRepository;
 public Employee GetEmployeeByUserName(Employee employee)
    {
        return _employeeRepository.Find()
                .Where(i => i.User_Name == employee.User_Name) 
                        as Employee;
    }

EmployeeRepository: EmployeeRepository:

 public IQueryable<T> Find()
    {
        var table = this.LookupTableFor(typeof(T));
        return table.Cast<T>();
    }

IRepository: IRepository:

public interface IRepository<T> where T: class
{
    void Commit();
    void Delete(T item);
    IQueryable<T> Find();
    IList<T> FindAll();
    void Add(T item);      

}

What gives??? 是什么赋予了???

The Find method does not expect a generic type and the where clause is returning an IEnumerable which you're attempting to cast as Employee. Find方法不期望泛型类型,而where子句返回您尝试强制转换为Employee的IEnumerable。 Replace the where clause with FirstOrDefault. 用FirstOrDefault替换where子句。 eg 例如

_employeeRepository.Find()
                .FirstOrDefault(i => i.User_Name == employee.User_Name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM