I have this class (you can see it in its entirety here)…
internal class BaseRepository<I, C> : IRepository<I>
where I : class, IBaseObject
where C : BaseObject
{
private Context _context;
public IEnumerable<I> FindBy(Expression<Func<I, bool>> predicate)
{
return _context.Set<C>().ToList().Cast<I>().AsQueryable().Where(predicate);
}
// other methods.
}
How can I work this so I don’t have to call .ToList() which I believe causes EF to return everything in .Set<C>()
Using this without .ToList() and .AsQueryable() causes the error:
System.NotSupportedException: Unable to cast the type ‘Sln.DAL.Sql.Entities.Project’ to type ‘Sln.DAL.Entities.IProject’. LINQ to Entities only supports casting EDM primitive or enumeration types.
If we add a constraint
Can we then use