Consider this scenario:
I have a reference data that I write service to return this to my clients.In my program I use this data in different types.
I want to get exactly what I want from database from client and in dynamic manner.
I want to use such this code:
public List<TResult> FindAll<T, TResult>(Func<T, bool> exp, Func<T, TResult> selector, int PageSize) where TResult : class
{
}
the problem is I can’t declare my service interface as generic and I can’t use that code this way:
public List<TResult> FindAll<Order, TResult>(Func<Order, bool> exp, Func<Order, TResult> selector, int PageSize) where TResult : class
{
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
return dc.Orders.Where(exp).Select<Order, TResult>(selector).ToList<TResult>();
}
}
Because:
-
Order in function act as parameter not Order class.
-
my TResult is not declared in service
How I can do this? Thanks a lot.
You cannot make your method generic, you cannot pass delegate as parameter and you probably cannot pass expression tree as parameter as well because I think it is by default not serializable. You should use some API which already do this for you – check WCF Data Services or Expression Tree Serialization.