简体   繁体   English

如何动态访问 DbSet 类型

[英]How to access DbSet type dynamically

I'm using LINQ expressions in order to build select and where expressions dynamically.我正在使用 LINQ 表达式来动态构建 select 和 where 表达式。

The select method looks like this:选择方法如下所示:

public Expression<Func<TSource, TTarget>> GetSelectExpression<TSource, TTarget>()
    {
        ParameterExpression l_pe = Expression.Parameter(Base_type, "source");  
...            
        Expression<Func<TSource, TTarget>> l_return = Expression.Lambda<Func<TSource, TTarget>>(l_select, new ParameterExpression[] { l_pe });
        l_return.Compile();

        return l_return;
    }

The Where method looks like the following: Where 方法如下所示:

public Expression<Func<TTarget, bool>> GetWhereExpression<TTarget>()
    {
        ParameterExpression l_pe = Expression.Parameter(Dto_type, "dto");

        ....

        Expression<Func<TTarget, bool>> l_return = Expression.Lambda<Func<TTarget, bool>>(l_or, new ParameterExpression[] { l_pe });
        l_return.Compile();

        return l_return;
    }

If I call the following statements, everything works fine:如果我调用以下语句,则一切正常:

var l_where = GetWhereExpression<CrudChangetaskDto>();
var l_select = GetSelectExpression<A_Changetask, CrudChangetaskDto>();               

IQueryable<CrudChangetaskDto> l_query = l_db.A_Changetask.Select(l_select).Where(l_where);
var lt_result = l_query.ToList();

Because of using the WHERE and SELECT methods more dynamically, I like to have a method returning an IQueryable like the following (PaltrConnect is the database context):由于更动态地使用 WHERE 和 SELECT 方法,我喜欢有一个返回 IQueryable 的方法,如下所示(PaltrConnect 是数据库上下文):

IQueryable<TTarget> GetSearchhelpvalue<TSource, TTarget>(PaltrConnect context)
    {   
        var l_dbset = context.Set(typeof(TSource));            

        IQueryable<TTarget> l_query = l_dbset.Select(GetSelectExpression<TSource,TTarget>()).Where(GetWhereExpression<TTarget>());
        return l_query;
    }

Unfortunately, this is syntactically not correct.不幸的是,这在语法上是不正确的。 I get the error message System.Linq.IQueryable cannot be converted into System.Linq.IQueryable.我收到错误消息 System.Linq.IQueryable 无法转换为 System.Linq.IQueryable。

Finally, I like to call GetSearchhelpvalue in the following way:最后,我喜欢通过以下方式调用 GetSearchhelpvalue:

using (var l_db = new PaltrConnect)
  {
    IQueryable<CrudChangeTaskDto> l_query = GetSearchhelpvalue<A_Changetask, CrudChangetaskDto>(l_db);
    var lt_result = l_query.ToList();
  }

How can I access the DbSet of the type which corresponds to TSource or how can this be solved?如何访问与 TSource 对应的类型的 DbSet 或者如何解决?

Yours Stephan你的斯蒂芬

You are so close.你是如此接近。 Just instead of the non generic DbContext.Set(Type) use the generic DbContext.Set<TEntity>() overload like this只是代替非通用DbContext.Set(Type)使用通用DbContext.Set<TEntity>() 重载这样

IQueryable<TTarget> GetSearchhelpvalue<TSource, TTarget>(PaltrConnect context)
    where TSource : class
{   
    return context.Set<TSource>()            
        .Select(GetSelectExpression<TSource,TTarget>())
        .Where(GetWhereExpression<TTarget>());
}

Note that it requires class constraint.请注意,它需要class约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM