【问题标题】:How pass the right side "where" to an IQueryable instantiation in c#?如何将右侧“where”传递给 c# 中的 IQueryable 实例化?
【发布时间】:2017-05-15 20:02:38
【问题描述】:

我想抽象我的 IQueryable 调用,以便我可以将 where 传递给函数:

类似:

public IQueryable<T> executeQuery<T>(T baseType, Expression<Func<T,object>> whereFunc) where T : class 
{
    //Get context
    DataContext dbContext = new DataContext(connection);

    //Get the table representation
    Table<T> baseTable = dbContext.GetTable<T>();

    //Get our query object
    IQueryable<T> baseQuery = from item in baseTable where whereFunc select item;
}

上述方法不起作用,但有什么方法可以做到这一点吗? (即一般创建 IQueryable 但允许传入 where 子句?)

【问题讨论】:

    标签: c# .net sql-server linq linq-to-sql


    【解决方案1】:

    您的whereFunc 签名错误。 Where 是一个过滤器,所以应该返回 bool,而不是 object。此外,在这种情况下,您不能使用简化的 linq 语法。这应该有效:

    public IQueryable<T> executeQuery<T>(T baseType, Expression<Func<T,bool>> whereFunc) where T : class 
    {
        //Get context
        DataContext dbContext = new DataContext(connection);
    
        //Get the table representation
        Table<T> baseTable = dbContext.GetTable<T>();
    
        //Get our query object
        IQueryable<T> baseQuery = baseTable.Where(whereFunc);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多