I would like to create a generic query from a generic class T. Is there a way to to something like that using reflection or something else?
public class DAO<T>
where T : class
{
protected ObjectSet<T> Entities
{
get
{
return myContextThatIsInSomewhere.CreateObjectSet<T>();
}
}
public IList<T> SelectBy(object fields)
{
if (fields == null)
{
throw new ArgumentNullException("fields");
}
var query = from e in this.Entities
select e;
foreach (var field in fields.GetType().GetFields())
{
query = from e in this.Entities
// Do something like that:
where e.(field.Name) == field.GetValue()
select e;
}
return query.ToList();
}
}
Make
SelectBytake anExpression<Func<T, bool>>(call itpredicate) and then you can just sayYou can pass an instance of
Expression<Func<T, bool>>by sayingfor example.