开发者

Problem with using Join operator in Lambda Expression and Expression Tree

开发者 https://www.devze.com 2023-03-01 14:58 出处:网络
I write this method: public List<TResult2> SelectAndJoin<TOuter, TInner, TKey, TResult, TResult2>(IEnumerable<TInner> inner,

I write this method:

public List<TResult2> SelectAndJoin<TOuter, TInner, TKey, TResult, TResult2>(IEnumerable<TInner> inner, 
                                                                      System.Linq.Expressions.Expression<Func<Regions, TKey>> outerKeySelector, 
                                                                      System.Linq.Expressions.Expression<Func<TInner, TKey>> innerKeySelector, 
                                                                      System.Linq.Expressions.Expression<Func<Regions, TInner, TResult>> resultSelector,
                                                                      Func<Regions, TResult2> selector)
    {
        using (RepositoryDataContext = new DataClasses1DataContext())
        {
                return RepositoryDataContext.Regions.Join(inner, outerKeySelector, innerKeySelector, resultSelector).AsEnumerable<TResult2>().Select<Regions, TResult2>(selector).ToList<TResult2>();

      开发者_运维问答  }
    }

but the expression follow return has this Error:

'System.Linq.IQueryable' does not contain a definition for 'AsEnumerable' and the best extension method overload 'System.Linq.Enumerable.AsEnumerable(System.Collections.Generic.IEnumerable)' has some invalid arguments

How I can get rid of this error?

is this code standard?

thanks


You're calling AsEnumerable<TResult2> on enumerable which will be of type IQueryable<TResult>. You should call AsEnumerable<TResult> or you can even omit generic parameter and call AsEnumerable()

Also your next select will not work for the same reason - you are providing wrong types for generics.


I might be missing the point... but:

IQueryable inherits from IEnumerable so I think that AsEnumerable() is unnecessary in this code.

If you really do need/want to execute the query before doing the Select, then you could use ToList() instead of AsEnumerable() - this would be clearer

I'm also wondering if you are including using System.Linq in your .cs file - as AsEnumerable() is an extension method within System.Linq

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号