Domanda

I've created the following extension.

Code

public static class QueryableExtensions
{
    public static QueryMapper<TSource> Map<TSource>(this IQueryable<TSource> source)
    {
        return new QueryMapper<TSource>(source);
    }

    public static ObjectMapper<TSource> Map<TSource>(this TSource obj)
    {
        return new ObjectMapper<TSource>(obj);
    }
}

How I call the above

return this.repository.FindAll()
           .OrderBy(o => o.Name)
           .Map().To<TEntityDto>();

Error

Cannot implicitly convert type 'TEntityDto' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

My original solution was to rename the method that takes the IQueryable as input to "MapQuery"...

But I'd like to have the same naming convention when calling it on an object or collection. I'm not sure how to go about adding constrains (or something) to limit what the calling/source object should look like. (EG. single class/collection of class)

È stato utile?

Soluzione

Adding this solved my problem, as the query chain I used it on had an order by and the resulting object was IOrderedQueryable, and not IQueryable.

public static QueryMapper<TSource> Map<TSource>(this IOrderedQueryable<TSource> source) 
{
   return new QueryMapper<TSource>(source); 
}

As a final thought...

I'm just wondering why would "Map(this TSource obj)", have preference over "Map(this IQueryable source)", as IOrderedQueryable has a base type of IQueryable?

Some more info on the topic

Generics and calling overloaded method from difference class - precedence issue

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top