score:1

Accepted answer
public IEnumerable<TEntity> Get<TEntity, TOrderBy>(Expression<Func<TEntity,TOrderBy>> orderBy)

score:2

The repository class itself should have a type parameter. Then you don't need to specify the entity type when accessing the repository members. Like this:

public interface IRep<TEntity>
{
    IEnumerable<TEntity> Get<TOrderBy>(
        Expression<Func<TEntity, TOrderBy>> orderBy
    );
}

An example entity class:

public class MyEntity
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

An example repository implementation:

public class Rep<TEntity> : IRep<TEntity> { ... }

Now you can just consume it like this:

var a = new Rep<MyEntity>(); 
var b = a.Get(x => x.Name); // string
var c = a.Get(x => x.Price); // decimal

This is how Linq does it. :)


Related Articles