【问题标题】:Is there a way to specify an operator as an argument in LINQ?有没有办法在 LINQ 中将运算符指定为参数?
【发布时间】:2013-06-06 00:32:45
【问题描述】:

我有一段这样的代码:

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

这只会给我那些以空字符串作为键值的记录。

要获得带有值的记录,所有改变的只是 == 到 !=

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

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

问候。

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    不完全是,但如果你稍微修改你的 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();
    

    这就是equals函数。

    我要做的就是将它们放入字典中:

    Dictionary<string, Func<string, string, bool>> selectorDictionary = 
        new Dictionary<string, Func<string, string, bool>>() 
            { {"==", (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();
    

    这比其他答案更好,因为它也可以扩展到其他运算符。

    【讨论】:

      【解决方案2】:

      我猜你正在寻找类似的东西:

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

      【讨论】:

        【解决方案3】:

        您可以通过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();
        

        【讨论】:

          【解决方案4】:

          类似的东西原则上应该有效..

          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();
          

          【讨论】:

          • 什么是IsStringOrEmpty
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-07-03
          • 2014-08-30
          • 1970-01-01
          • 2013-01-07
          • 2015-06-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多