如何使LINQ表达式接收参数到通用存储库中的LINQ查询

发布于 2025-01-23 14:57:10 字数 1938 浏览 3 评论 0原文

当我尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2025-01-30 14:57:10

解决方案是修复查询,并使用其中的滤镜过载,以添加另一个滤波器

 public async Task<IEnumerable<TResult>> Join<TIncludeProperty, TEntity, TResult>(IEnumerable<T> collection,Expression<Func<T,TIncludeProperty>> includeClause, 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(y=> collection.Contains(y)).Select(resultSelector).ToListAsync();
        }
        catch (Exception ex)
        {
            return Enumerable.Empty<TResult>();
        }
    } 

   public async Task<IEnumerable<TResult>> Join<TIncludeProperty, TEntity, TResult>(IEnumerable<T> collection, Expression<Func<T, TIncludeProperty>> includeClause, Expression<Func<T, TResult>> resultSelector, Expression<Func<T, bool>> whereFilter)
      where TEntity : class, new()
       where TResult : class, new()
        where TIncludeProperty : class, new()
    {
        try
        {
            
            return await Db.Set<T>().Include(includeClause).Where(x=> collection.Contains(x)).Where(whereFilter).Select(resultSelector).ToListAsync();
        }
        catch (Exception ex)
        {
            return Enumerable.Empty<TResult>();
        }
    }

the solution was fixing the query and using a where filter also overloading to add another where filter

 public async Task<IEnumerable<TResult>> Join<TIncludeProperty, TEntity, TResult>(IEnumerable<T> collection,Expression<Func<T,TIncludeProperty>> includeClause, 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(y=> collection.Contains(y)).Select(resultSelector).ToListAsync();
        }
        catch (Exception ex)
        {
            return Enumerable.Empty<TResult>();
        }
    } 

   public async Task<IEnumerable<TResult>> Join<TIncludeProperty, TEntity, TResult>(IEnumerable<T> collection, Expression<Func<T, TIncludeProperty>> includeClause, Expression<Func<T, TResult>> resultSelector, Expression<Func<T, bool>> whereFilter)
      where TEntity : class, new()
       where TResult : class, new()
        where TIncludeProperty : class, new()
    {
        try
        {
            
            return await Db.Set<T>().Include(includeClause).Where(x=> collection.Contains(x)).Where(whereFilter).Select(resultSelector).ToListAsync();
        }
        catch (Exception ex)
        {
            return Enumerable.Empty<TResult>();
        }
    }
痴情 2025-01-30 14:57:10

嗨,您可以使用此逻辑
,,,,,,

 public virtual IEnumerable<TEntity> Get(Expression<Func<TEntity,bool>>  Where=null)
        {
            IQueryable<TEntity> query = _dbSet;
            if(Where != null)
            {
                query = query.Where(Where);
            }
            return query.ToList();
        }


我希望这对你有帮助

hi you can use this logic
,,,

 public virtual IEnumerable<TEntity> Get(Expression<Func<TEntity,bool>>  Where=null)
        {
            IQueryable<TEntity> query = _dbSet;
            if(Where != null)
            {
                query = query.Where(Where);
            }
            return query.ToList();
        }

,,,
i hope this help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文