Generic extensions method for IQueryable<out T>
Honestly I hate extension methods as disscussed before.
I have a extension method as "ApplySorting"
public static IQueryable<Brand> ApplySorting(this IQueryable<Brand> data,
IList<GroupDescriptor> groupDescriptors,
IList<SortDescriptor> sortDescriptors)
It has been implemented for IQueryable<Brand>
it is look like nice. I need another method for Category, I should implement another method as IQueryable<Category>
and this story goes.
Question; How can I define IQueryable<T>
to stop repeated extension method?
It should be look like this (of course it is not valid code VS 2010 stoped me.)
public static IQueryable<T> ApplySorting(this IQueryable<T> data,
IList<GroupDescriptor> groupDescriptors,
IList<SortDescriptor> sortDescripto开发者_StackOverflowrs)
where T : IEntity
Thanks in advance! :)
Try this:
public static IQueryable<T> ApplySorting<T>(this IQueryable<T> data,
IList<GroupDescriptor> groupDescriptors,
IList<SortDescriptor> sortDescriptors)
where T : IEntity
(I added a type variable to the method)
You need to make the method generic in order to have generic parameters.
In other words, this will work:
public static IQueryable<T> ApplySorting<T>(this IQueryable<T> data,
IList<GroupDescriptor> groupDescriptors,
IList<SortDescriptor> sortDescriptors)
where T : IEntity
(Note that <T>
is present in method's name)
All LINQ extension methods are defined this way, but in most cases passing the concrete type is optional due to type inference, that's why it's easy to miss it.
精彩评论