Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq where call是否减少对我的数据库的调用(自定义构建)_C#_Linq_Yield - Fatal编程技术网

C# linq where call是否减少对我的数据库的调用(自定义构建)

C# linq where call是否减少对我的数据库的调用(自定义构建),c#,linq,yield,C#,Linq,Yield,我有一个从数据库中获取行的方法。看起来是这样的: public static IEnumerable<Dictionary<string, object>> GetRowsIter() { _resultSet.ReadFirst(); do { var resultList = new Dictionary<string, object>(); for (int fieldIndex = 0; fiel

我有一个从数据库中获取行的方法。看起来是这样的:

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;
}
public静态IEnumerable GetRowsIter()
{
_resultSet.ReadFirst();
做
{
var resultList=新字典();
对于(int fieldIndex=0;fieldIndex<\u resultSet.FieldCount;fieldIndex++)
{
添加(_resultSet.GetName(fieldIndex),
_resultSet.GetValue(fieldIndex));
}
收益回报结果列表;
}而(_resultSet.ReadRelative(1));
屈服断裂;
}
当我想返回所有行时,这非常好。但有时我只想返回部分行

我计划写我自己的方法(
GetRowsIterWhere(string-column,object-whereValue)
),但我想知道我是否可以在我的方法上使用linq-where

我承认我不知道它是如何工作的,因为我正在用ReadRelative(1)推进读者,以获得下一个值。因此,即使它“认为”它正在跳过行,它也不会真正跳过它们

我非常关心这里的性能(我目前正在从Linq重构到数据集,因为它太慢了。)


因此,我的问题是,我是否需要编写自己的
Where
类型方法,或者我是否可以将上面的方法更改为使用linq
Where
方法?

是的,您可以使用linq
Where
,但您需要自己构建谓词。这并不棘手。类似于(从内存中;没有编译器):

var param=Expression.Parameter(typeof(T),“row”);
var body=表达式。等于(
Expression.PropertyOrField(参数,列),
表达式。常量(whereValue));
var lambda=表达式.lambda(body,param);
然后:

IQueryable源=。。。
var filtered=来源,其中(λ);

这将导致在服务器上执行
Where
(例如,在TSQL中),删除大部分网络IO(使用合理的过滤器)。

WOW。我认为我在C#是公平的,但我甚至不知道在哪里插上这个。“在我使用它之前,我必须做一些研究。”瓦卡诺-你的评论;例如,对于LINQ to SQL,
yourDataContext.SomeTable
是一个
IQueryable
var param = Expression.Parameter(typeof(T), "row");
var body = Expression.Equal(
               Expression.PropertyOrField(param, column),
               Expression.Constant(whereValue));
var lambda = Expression.Lambda<Func<T,bool>>(body, param);
IQueryable<T> source = ...
var filtered = source.Where(lambda);