Linq存储库和GetTable< T>() [英] Linq repository and GetTable<T>()
问题描述
我遵循相当标准的L2S存储库模式,将以下内容用作方法之一
I'm following the fairly standard L2S repository pattern, using the following as one of the methods
public IEnumerable<T> GetAllByFilter(Func<T, bool> expression)
{
return _dataContext.GetTable<T>().Where(expression);
}
我很想知道对GetTable
的调用似乎是从字面上获取表的,而Where
表达式大概是在内存中求值的.
I'm a bit miffed to see that the call to GetTable
appears to literally get the table, with the Where
expression presumably evaluated in-memory afterwards.
像这样的简单呼叫
var order = GetAllByFilter(o => o.OrderNumber == 1);
应该只返回一条记录的
正在获取整个50000条记录数据库.
which should only ever return one record, is fetching the entire 50000 record database.
Linq通常这么不好吗?还是我错过了什么?
Is Linq normally this bad? Or am I missing something?
推荐答案
更改:
public IEnumerable<T> GetAllByFilter(Func<T, bool> expression)
{
return _dataContext.GetTable<T>().Where(expression);
}
收件人:
public IQueryable<T> GetAllByFilter(Expression<Func<T, bool>> expression)
{
return _dataContext.GetTable<T>().Where(expression);
}
这将使用Queryable
(即SQL)而不是Enumerable
(即本地),因此效果会更好.
This will use Queryable
(i.e. SQL) instead of Enumerable
(i.e. local) and therefore will perform much better.
这篇关于Linq存储库和GetTable< T>()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!