How do interfaces work inside generic type parameters?

Viewed 45

I want to create a method like the following:

public IQueryResult<TEntity2> SwapQueryEntity<TEntity2>(Expression<Func<TEntity, List<TEntity2>>> entitySwapMap)
{
       return new QueryResult<TEntity2>(_query.SelectMany<TEntity, TEntity2>(entitySwapMap));
}

Where _query is of type IQueryable<TEntity>.

However I am getting the following error on the parameter to the SelectMany method:

Cannot convert from System.Linq.Expressions.Expression<System.Func<TEntity, System.Collections.Generic.List<TEntity2>>> to System.Linq.Expressions.Expression<System.Func<TEntity, System.Collections.Generic.IEnumerable<TEntity2>>>

So the issue seems to be that it wont recognise the result type of the expression of List<TEntity2> to be assignable to IEnumerable<TEntity2> despite the fact that List<T> implements IEnumerable<T>.

I am not seeking an answer as to how to make my code "work". I could simply change the method signature to use IEnumerable instead of List and my problem goes away.

What I would like to gain is a deeper understanding of why this issue is occuring and why the compiler won't recognise a List as an IEnumerable in this case?

0 Answers
Related