如何使LINQ表达式接收参数到通用存储库中的LINQ查询
当我尝试使用iEnumerable系列使用DBSET创建一个加入时,它会引发以下例外,因此我认为我可以包括一个属性,然后通过此属性进行过滤并选择它...
system.invalidoperationException:'linq表达式dbset() 。加入( 内部:__p_0, OuterKeySelecter:y => y.idtoken, InnerKeySelector:z => Z.Idtoken, ResultSelector :(令牌,Usertokens)=>代币)'无法翻译。可以通过可以翻译的形式重写查询,或者通过插入“可忽略的”,“ asasyncenumerable”,“ tolist”或“ tolistAsync”来明确切换到客户端评估。请参阅 https://go.microsoft.com/fwlink/?有关更多信息。
public async Task<IEnumerable<TResult>> Join<Tkey, TEntity, VKey, TResult>(IEnumerable<TEntity> collection, Expression<Func<T, Tkey>> outerSelector, Expression<Func<TEntity, Tkey>> innerSelector, Expression<Func<T, TEntity, TResult>> resultSelector)
where TEntity : class, new()
where TResult:class,new()
{
return await Db.Set<T>().Join(collection, outerSelector, innerSelector, resultSelector).ToListAsync();
}
现在,我要使用的查询是这样的,
public async Task<IEnumerable<TResult>> Join<Tkey,TIncludeProperty, TEntity, TResult>(IEnumerable<T> collection,Expression<Func<T,TIncludeProperty>> includeClause, Func<T, IEnumerable<T>, bool> whereFilter, Expression<Func<T, TResult>> resultSelector)
where TEntity : class, new()
where TResult :class,new()
where TIncludeProperty: class,new()
{
try
{
return await Db.Set<T>().Include(includeClause).Where(x=>whereFilter(x,collection)).Select(resultSelector).ToListAsync();
}
catch (Exception ex)
{
return Enumerable.Empty<TResult>();
}
}
我该如何传递到该方法签名上的集合的位置?
When I am trying to create a join with a IEnumerable Collection with a DbSet it throws the following exception,so i am thinking i could include a property then filter by this property I need and select it...
System.InvalidOperationException: 'The LINQ expression 'DbSet()
.Join(
inner: __p_0,
outerKeySelector: y => y.IdToken,
innerKeySelector: z => z.IdToken,
resultSelector: (tokens, userTokens) => tokens)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
public async Task<IEnumerable<TResult>> Join<Tkey, TEntity, VKey, TResult>(IEnumerable<TEntity> collection, Expression<Func<T, Tkey>> outerSelector, Expression<Func<TEntity, Tkey>> innerSelector, Expression<Func<T, TEntity, TResult>> resultSelector)
where TEntity : class, new()
where TResult:class,new()
{
return await Db.Set<T>().Join(collection, outerSelector, innerSelector, resultSelector).ToListAsync();
}
now the query I want to use is like this
public async Task<IEnumerable<TResult>> Join<Tkey,TIncludeProperty, TEntity, TResult>(IEnumerable<T> collection,Expression<Func<T,TIncludeProperty>> includeClause, Func<T, IEnumerable<T>, bool> whereFilter, Expression<Func<T, TResult>> resultSelector)
where TEntity : class, new()
where TResult :class,new()
where TIncludeProperty: class,new()
{
try
{
return await Db.Set<T>().Include(includeClause).Where(x=>whereFilter(x,collection)).Select(resultSelector).ToListAsync();
}
catch (Exception ex)
{
return Enumerable.Empty<TResult>();
}
}
how can i pass to whereFilter the collection on the method's signature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案是修复查询,并使用其中的滤镜过载,以添加另一个滤波器
the solution was fixing the query and using a where filter also overloading to add another where filter
嗨,您可以使用此逻辑
,,,,,,
我希望这对你有帮助
hi you can use this logic
,,,
,,,
i hope this help you