2013-06-10 2 views
0

는 그래서 같은 코드의 조각을 가지고이 나에게 키 값으로 빈 문자열을 만 RECS을 제공LINQ에서 연산자를 연산자로 지정하는 방법이 있습니까?

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

.

는 그래서 ==에서 비교를 변경하는 방법! = 기반으로하는이 코드 조각을 넣을 수 있습니다 =

를 값으로 ==이 변경 모든!와 RECS를 얻으려면 무엇이 필요한지 또는 이렇게하려면 쿼리를 반복합니까?

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

감사합니다.

답변

1

나는 당신이 그런 일을 찾고있는 것 같아요 : 당신은 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(); 
0

,하지만 당신은 약간 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(); 

이것은 다른 답변보다 더, 너무. 그런

3

의 결과를 그렇진 비교할 수

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

뭔가

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

'IsStringOrEmpty' 무엇입니까 .. 원칙적으로 일을해야합니까? –

관련 문제