如何使Linq表达式在通用存储库中接收linq查询参数

xxe27gdn  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(105)

当我试图创建一个连接与IEEECollection与DbSet它抛出以下异常,所以我想我可以包括一个属性,然后过滤这个属性,我需要和选择它。
System.InvalidOperationException:'LINQ表达式'DbSet().Join(inner:__p_0,outerKeyToken:y => y.IdToken,innerKeyToken:z => z.IdToken,resultToken:(tokens,userTokens)=> tokens)'无法转换。请以可转换的形式重写查询,或通过插入对' AsEnumerable ',' AsAsyncEnumerable ',' ToList ',' AsEnumerable ',' AsEnumerable ',' ToList ',' AsEnumerable '的调用,或'ToListAsync'。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038。'

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>();
        }
    }


我如何将方法签名上的集合传递给whereFilter?

rqmkfv5c

rqmkfv5c1#

解决方案是修复查询并使用where过滤器,同时重载以添加另一个where过滤器

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>();
        }
    }

字符串

zpqajqem

zpqajqem2#

你可以用这个逻辑,

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();
        }

字符串
。希望这能帮到你

相关问题