Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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中指定运算符作为参数?_C#_Linq - Fatal编程技术网

C# 有没有办法在LINQ中指定运算符作为参数?

C# 有没有办法在LINQ中指定运算符作为参数?,c#,linq,C#,Linq,我有这样一段代码: var emptyKeys = (from recs in allRecords where recs.Key == string.Empty orderby recs.Key select recs).ToList(); 这只提供了那些以空字符串作为键值的REC 要获取具有值的REC,所有更改都是==To= 那么,是否可以将

我有这样一段代码:

var emptyKeys = (from recs in allRecords
                         where recs.Key == string.Empty
                         orderby recs.Key
                         select recs).ToList();
这只提供了那些以空字符串作为键值的REC

要获取具有值的REC,所有更改都是==To=

那么,是否可以将这段代码放在一个方法中,该方法将比较从==更改为!=根据所需内容或我是否重复查询以这样做:

var emptyKeys = (from recs in allRecords
                         where recs.Key != string.Empty
                         orderby recs.Key
                         select recs).ToList();

问候。

我想你在找这样的东西:

function GetRecs(bool EmptyKey)    
{    
   var Keys = (from recs in allRecords
                         where EmptyKey == (recs.Key == string.Empty)
                         orderby recs.Key
                         select recs).ToList();
   return Keys;     
}

我想你是在找这样的东西:

function GetRecs(bool EmptyKey)    
{    
   var Keys = (from recs in allRecords
                         where EmptyKey == (recs.Key == string.Empty)
                         orderby recs.Key
                         select recs).ToList();
   return Keys;     
}

您可以传递
isEmpty
过滤器,并将其与
recs.Key==String.Empty的结果进行比较

bool isEmpty = true;
var keys = (from recs in allRecords
            where (recs.Key == String.Empty) == isEmpty
            orderby recs.Key
            select recs).ToList();

您可以传递
isEmpty
过滤器,并将其与
recs.Key==String.Empty的结果进行比较

bool isEmpty = true;
var keys = (from recs in allRecords
            where (recs.Key == String.Empty) == isEmpty
            orderby recs.Key
            select recs).ToList();

不完全正确,但如果稍微修改LINQ查询,可以执行以下操作:

Func<string, string, bool> selectorFunc = (a, b) => a == b;
var emptyKeys = (from recs in allRecords
                         where selectorFunc(recs.Key, string.Empty)
                         orderby recs.Key
                         select recs).ToList();
Func选择器Func=(a,b)=>a==b;
var emptyKeys=(来自所有记录中的记录)
其中selectorFunc(recs.Key,string.Empty)
orderby记录键
选择recs.ToList();
这就是equals函数

我要做的是把它们放在字典里:

Dictionary<string, Func<string, string, bool>> selectorDictionary = 
    new Dictionary<string, Func<string, string, bool>>() 
        { {"==", (a, b) => a == b}, {"!=", (a, b) => a != b} };
字典选择器字典=
新字典()
{{a,b=>a==b},{a,b=>a!=b},{a,b=>a!=b};
然后像这样使用它:

Dictionary<string, Func<string, string, bool>> selectorDictionary = 
    new Dictionary<string, Func<string, string, bool>>() 
        { {"==", (a, b) => a == b}, {"!=", (a, b) => a != b} };
Func<string, string, bool> selectorFunc = selectorDictionary[operator];
var emptyKeys = (from recs in allRecords
                         where selectorFunc(recs.Key, string.Empty)
                         orderby recs.Key
                         select recs).ToList();
字典选择器字典=
新字典()
{{a,b=>a==b},{a,b=>a!=b},{a,b=>a!=b};
Func-selectorFunc=selectorDictionary[运算符];
var emptyKeys=(来自所有记录中的记录)
其中selectorFunc(recs.Key,string.Empty)
orderby记录键
选择recs.ToList();

这比其他答案要好,因为它也可以扩展到其他操作符。

不太好,但是如果稍微修改LINQ查询,您可以执行以下操作:

Func<string, string, bool> selectorFunc = (a, b) => a == b;
var emptyKeys = (from recs in allRecords
                         where selectorFunc(recs.Key, string.Empty)
                         orderby recs.Key
                         select recs).ToList();
Func选择器Func=(a,b)=>a==b;
var emptyKeys=(来自所有记录中的记录)
其中selectorFunc(recs.Key,string.Empty)
orderby记录键
选择recs.ToList();
这就是equals函数

我要做的是把它们放在字典里:

Dictionary<string, Func<string, string, bool>> selectorDictionary = 
    new Dictionary<string, Func<string, string, bool>>() 
        { {"==", (a, b) => a == b}, {"!=", (a, b) => a != b} };
字典选择器字典=
新字典()
{{a,b=>a==b},{a,b=>a!=b},{a,b=>a!=b};
然后像这样使用它:

Dictionary<string, Func<string, string, bool>> selectorDictionary = 
    new Dictionary<string, Func<string, string, bool>>() 
        { {"==", (a, b) => a == b}, {"!=", (a, b) => a != b} };
Func<string, string, bool> selectorFunc = selectorDictionary[operator];
var emptyKeys = (from recs in allRecords
                         where selectorFunc(recs.Key, string.Empty)
                         orderby recs.Key
                         select recs).ToList();
字典选择器字典=
新字典()
{{a,b=>a==b},{a,b=>a!=b},{a,b=>a!=b};
Func-selectorFunc=selectorDictionary[运算符];
var emptyKeys=(来自所有记录中的记录)
其中selectorFunc(recs.Key,string.Empty)
orderby记录键
选择recs.ToList();

这比其他答案要好,因为它也可以扩展到其他操作符。

类似的东西原则上应该可以工作

Func<bool> notEmpty = (Key) => {return !Key.IsNullOrEmpty();}
Func<bool> empty = (Key) => {return Key.IsNullOrEmpty();}

Func<bool> comparer = notEmpty

var emptyKeys = (from recs in allRecords
                         where comparer
                         orderby recs.Key
                         select recs).ToList();
Func notEmpty=(Key)=>{return!Key.IsNullOrEmpty();}
Func empty=(Key)=>{return Key.IsNullOrEmpty();}
Func comparer=notEmpty
var emptyKeys=(来自所有记录中的记录)
比较器在哪里
orderby记录键
选择recs.ToList();

原则上这样做应该行得通

Func<bool> notEmpty = (Key) => {return !Key.IsNullOrEmpty();}
Func<bool> empty = (Key) => {return Key.IsNullOrEmpty();}

Func<bool> comparer = notEmpty

var emptyKeys = (from recs in allRecords
                         where comparer
                         orderby recs.Key
                         select recs).ToList();
Func notEmpty=(Key)=>{return!Key.IsNullOrEmpty();}
Func empty=(Key)=>{return Key.IsNullOrEmpty();}
Func comparer=notEmpty
var emptyKeys=(来自所有记录中的记录)
比较器在哪里
orderby记录键
选择recs.ToList();

什么是
IsStringOrEmpty
?什么是
IsStringOrEmpty