在 Lambda 表达式和表达式树中使用 Join 运算符时出现问题
我写了这个方法:
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>();
}
}
但是 return 后面的表达式有这个错误:
“System.Linq.IQueryable”不包含“AsEnumerable”的定义,并且最佳扩展方法重载“System.Linq.Enumerable.AsEnumerable(System.Collections.Generic.IEnumerable)”具有一些无效参数
我如何摆脱这个错误?
这个代码是标准的吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在对类型为
IQueryable
的枚举调用AsEnumerable
。您应该调用AsEnumerable
或者您甚至可以省略泛型参数并调用AsEnumerable()
另外,您的下一个选择将因同样的原因而不起作用 - 您提供了错误的类型对于仿制药。
You're calling
AsEnumerable<TResult2>
on enumerable which will be of typeIQueryable<TResult>
. You should callAsEnumerable<TResult>
or you can even omit generic parameter and callAsEnumerable()
Also your next select will not work for the same reason - you are providing wrong types for generics.
我可能没有抓住重点......但是:
IQueryable
继承自IEnumerable
所以我认为AsEnumerable()
在这段代码中是不必要的。如果您确实需要/想要在执行
Select
之前执行查询,那么您可以使用ToList()
而不是AsEnumerable()
-这会更清楚我还想知道您是否在 .cs 文件中包含
using System.Linq
- 因为AsEnumerable()
是System 中的扩展方法.Linq
I might be missing the point... but:
IQueryable
inherits fromIEnumerable
so I think thatAsEnumerable()
is unnecessary in this code.If you really do need/want to execute the query before doing the
Select
, then you could useToList()
instead ofAsEnumerable()
- this would be clearerI'm also wondering if you are including
using System.Linq
in your .cs file - asAsEnumerable()
is an extension method withinSystem.Linq