Domanda

I have a known working Repository. Using structuremap as IOC.

But I can't do any Iqueryable searches using the following:

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

EmployeeRepository:

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

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???

È stato utile?

Soluzione

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. Replace the where clause with FirstOrDefault. e.g.

_employeeRepository.Find()
                .FirstOrDefault(i => i.User_Name == employee.User_Name)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top