0

我正在使用类似模式的通用存储库来获取数据。有 100 多个实体,因此为每个实体创建单独的存储库并不是一个真正的选择。以下是同一类的一些函数:

    public int Count(Func<TEntity, bool> x=null)
    {
        return x == null ?
            mgr.CTX.GetAll<TEntity>().Count() :
            mgr.CTX.GetAll<TEntity>().Where(x).Count();
    }

    public TEntity One(Func<TEntity, bool> x)
    {
        return mgr.CTX.GetAll<TEntity>().Where(x).Take(1).FirstOrDefault();
    }

    public IQueryable<TEntity> All(Func<TEntity, bool> x=null)
    {
        return x == null ?
            mgr.CTX.GetAll<TEntity>() :
            mgr.CTX.GetAll<TEntity>().Where(x).AsQueryable<TEntity>();
    }

问题是不管调用哪个函数,Sql profiler显示都是一样的

从 [表格] 中选择 [列]

我想在使用 Take(1) 或 Count() 或 Where() 时,应使用Select的Count()TopWhere子句相应地进行查询,但这些函数对查询生成绝对没有影响。显然,从服务器获取所有数据后,每个操作似乎都在内存中执行。

如果我访问它的方式有问题或者这是 Telerik 的正常行为,请指导我?

4

1 回答 1

1

我相信您是 LINQ 扩展方法定义之间细微差别的受害者 - 内存中的方法使用Func<>而 SQL 绑定的方法Expression<>用作参数类型。
我的建议是All(Func<TEntity, bool> x=null)改为All(Expression<Func<TEntity, bool>> x=null)

于 2014-12-29T08:45:49.460 回答