Reputation: 902
I have this functionality:
public class Repository : IRepository
{
public List<TEntity> GetOrdered<TEntity, TSortKey>(Func<TEntity, TSortKey> orderBy, int take, params string[] includePaths) where TEntity : AbstractEntity
{
var query = (from ent in this.Context.Set<TEntity>() select ent).IncludePaths(includePaths);
return query.OrderBy(orderBy).Take(take).ToList();
}
}
To invoke it:
List<Project> p = repository.GetOrdered<Project, string>(x => x.Name, 10);
I want to eliminate the need to give it the second generic parameter when invoking, it's a deal breaker from an API perspective.
How to do it?
Upvotes: 1
Views: 89
Reputation: 152521
it's a deal breaker from an API perspective
then consider your deal broken... You can't partially infer generic parameters. If your repository was generic you could possibly do:
public class Repository<TEntity> : IRepository<TEntity> where TEntity : AbstractEntity
{
public List<TEntity> GetOrdered<TSortKey>(Func<TEntity, TSortKey> orderBy, int take, params string[] includePaths)
{
var query = (from ent in this.Context.Set<TEntity>() select ent).IncludePaths(includePaths);
return query.OrderBy(orderBy).Take(take).ToList();
}
}
then do
List<Project> p = repository.GetOrdered(x => x.Name, 10);
But I don't know if that change is possible for you.
Upvotes: 3
Reputation: 703
Either the compiler can infer all type parameters, or you have to specify them all. But you may explicitly specify the argument type of the lambda expression, such as:
List<Project> p = repository.GetOrdered((Project x) => x.Name, 10);
Upvotes: 1