I’m trying to extend a method that already has a return type. For example:
IQueryable<int> GetCategoryIds()
So, I can use this as follows:
IQueryable<int> categories = GetCategoryIds();
And I’m currently doing:
IQueryable<int> categories = GetCategoryIds().Select(c => new Client() { ClientId == c.clientId });
This works, but I don’t want to do that Select statement. I’d rather do an extension method that excepts a type so I can use Reflection in the extension method to determine the type etc. and return the results according. The extension method would be something like:
public static IQueryable<T> LovelyExtension(this T objectType, int clientId)
Then I can use this extension like (hopefully):
IQueryable<int> categories = GetCategoryIds().LovelyExtension<int>(1);
Is this possible? When I tried it, I got an error because the GetCategoryIds return type wasn’t the same as the extension method. But, this works with the Select statement.
You do not need to write an extension on
Type(your example is not doing it either). You need an extension onIQueryable<T>. You can do it like this: