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. :)
Source: stackoverflow.com
Related Articles
- LINQ Expression help with Func TEntity,TType
- How to use a Func in an expression with Linq to Entity Framework?
- Help with LINQ Expression
- Help with Linq expression to return a list of strings based on field count
- How to use an expression with a generic func lambda in a linq where clause?
- Help with recursive linq expression
- help with expression for nHibernate linq provider extension
- Help with Linq Expression - INotifyPropertyChanged
- Help With Generic LINQ OrderBy Lambda Expression
- Saved projection expression for re-use in different linq expressions with two source objects
- How to flatten nested objects with linq expression
- Error: "The specified LINQ expression contains references to queries that are associated with different contexts"
- How does LINQ expression syntax work with Include() for eager loading
- LINQ ToListAsync expression with a DbSet
- Selecting multiple columns with linq query and lambda expression
- How do I create a Linq expression tree with an F# lambda?
- C# re-use LINQ expression for different properties with same type
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- C# linq expression in lambda with contains
- Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression
- How to get a value out of a Span<T> with Linq expression trees?
- Create a Linq Expression with StartsWith, EndsWith and Contains passing a Expression<Func<T, string>>
- Can we control LINQ expression order with Skip(), Take() and OrderBy()
- C# LINQ build expression with anonymous type
- How can you update a Linq Expression with additional parameters?
- Covariance/Contravariance with a linq expression
- Help with LINQ distinct()
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ with Lambda expression - Join, Group By, Sum and Count
- Linq expression with nullable
- Can I use LINQ GroupBy to do this more cleanly?
- How to group dates and associated state
- Can i have only one method to delete record from passed table name and id using Entity Framework and Linq?
- C# Linq and Regexing non-unicode
- Correct Join statement in Linq
- Get values by property name from an object at different levels
- LINQ to XML Grouping
- LEFT JOIN with LINQ produces NULL reference exception
- How to select a column after GroupBy in LINQ?
- List in asp.net linq query not working
- Linq Query relating Many to Many relationship
- How to get differences that caused Except to add an IEnumerable?
- Dynamically Generating a Linq/Lambda Where Clause
- Linq getting average on a property of a List<class>
- Asp.net - how does it handle references to database rows?
- Expanding wildcards in a csv string to generate a collection of csv strings?
- Will Linq query code in C# UWP project work slower or faster depending on order of its' parts?
- Is it possible to have an out ParameterExpression?
- Unable to reassign values in a linq statement
- LINQ - order by results of subset