导航查询,如果封装成仓储,传入泛型参数,会报错 返回

SqlSugar 待处理
367
该叫什么 布衣 发布于2026/1/22
悬赏:5 飞吻

a306327e-bd6e-4370-9cae-b6e9e3cd5294.png

运行第一个红色框,会提示  Object does not match target type

而运行第二个框直接使用includes可以得到数据



以下是封装的方法:  
/// <summary>

  /// 功能描述:查询数据列表(支持导航查询) 

  /// </summary>

  /// <param name="whereExpression">查询条件表达式</param>

  /// <param name="includes">导航属性表达式,如 x => x.customer</param>

  /// <returns>数据列表</returns>

  public async Task<List<TEntity>> QueryWithInclude(Expression<Func<TEntity, bool>> whereExpression, params   Expression<Func<TEntity, dynamic>>[] includes)

  {

       

      var query = _db.Queryable<TEntity>();

      // 添加导航属性包含

      if (includes != null && includes.Length > 0)

      {

          foreach (var include in includes)

          {

              query = query.Includes(include);

          }

      }

      if (whereExpression == null) 

      {

          throw new ArgumentNullException(nameof(whereExpression), "查询操作必须指定条件,否则会导致整个查询整个表");

      }

      var res= await query.Where( whereExpression).ToListAsync(); 

      return res;

  }


热忱回答0