linq where call 是否减少对我的数据库的调用(自定义构建)
我有一个从数据库中获取行的方法。它看起来像这样:
public static IEnumerable<Dictionary<string, object>> GetRowsIter()
{
_resultSet.ReadFirst();
do
{
var resultList = new Dictionary<string, object>();
for (int fieldIndex = 0; fieldIndex < _resultSet.FieldCount; fieldIndex++)
{
resultList.Add(_resultSet.GetName(fieldIndex),
_resultSet.GetValue(fieldIndex));
}
yield return resultList;
} while (_resultSet.ReadRelative(1));
yield break;
}
当我想返回所有行时,这非常有用。但有时我只想返回某些行。
我计划编写自己的方法 (GetRowsIterWhere(string column, object whereValue)
),但我想知道是否可以在我的方法中使用 linq where 。
我承认我不知道它是如何工作的,因为我正在通过 ReadRelative(1) 推动读者获得下一个值。因此,即使它“认为”它正在跳过行,它也不会真正跳过它们。
我真的很关心这里的性能(我目前正在从 Linq-To-Datasets 进行重构,因为它太慢了。)
所以,我的问题是,我是否需要编写自己的 Where
类型方法或者我可以更改上面的方法以使用 linq where
方法吗?
I have a method that gets rows from my database. It looks like this:
public static IEnumerable<Dictionary<string, object>> GetRowsIter()
{
_resultSet.ReadFirst();
do
{
var resultList = new Dictionary<string, object>();
for (int fieldIndex = 0; fieldIndex < _resultSet.FieldCount; fieldIndex++)
{
resultList.Add(_resultSet.GetName(fieldIndex),
_resultSet.GetValue(fieldIndex));
}
yield return resultList;
} while (_resultSet.ReadRelative(1));
yield break;
}
This is great when I want to return all the rows. But sometimes I want to return only some of the rows.
I am planning on writing my own method (GetRowsIterWhere(string column, object whereValue)
), but I am wondering if I can use the linq where on my method.
I admit I don't know how it would work, becuase I am advancing the reader with a ReadRelative(1) to get to the next value. So even if it "thinks" it is skipping rows, it will not really skip them.
I am really concerned with performance here (I am currently refactoring from Linq-To-Datasets because it was way way way too slow.)
So, my question is, do I need to write my own Where
type method or can I change the one above to work with the linq where
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以使用 LINQ
Where
,但您需要自己构建谓词。这并不棘手。类似于(从内存中;没有手头的编译器):then:
这将导致
Where
在服务器上执行(例如,在 TSQL 中),删除大部分网络 IO(假设有一个合理的网络 IO)筛选)。Yes you can use LINQ
Where
, but you'll need to build the predicate yourself. It isn't tricky. Something like (from memory; no compiler to hand):then:
This will cause the
Where
to be executed at the server (for example, in TSQL), removing most of the network IO (asusming a sensible filter).