2017-05-10 101 views
3

如何在執行下面的查詢時忽略NULL和空字符串匹配?在Linq查詢中忽略空和空字符串匹配

var tMisMatchList = lstExchgMatch.Any(o => 
    o.MulDivFlg != lstExchgMatch[0].MulDivFlg); 

例如,名單低於數據

[0] = MulDivFlg = "";  // 1st element in the list whose multdivflag value is "" 
    [1] = MulDivFlg = null; // 2nd element in the list whose multdivflag value is null 
    [2] = MulDivFlg = null; 
    [3] = MulDivFlg = ""; 

在這種情況下,我上面的查詢返回true。因爲「」和NULL不匹配已經發生。但我的期望是忽略空和「」比較檢查。它只會執行不可爲空和非空字符串匹配。它應該忽略null和「」的存在並將它們視爲相等

上面的查詢返回false,因爲它考慮的是null不等於「」。 但我希望它返回true,並認爲null和「」等於或忽略該字符串爲null或「」。

+0

請加什麼是現在的結果,什麼結果你希望收到 –

+0

見[這裏](http://stackoverflow.com/questions/9606979 /串isnullorwhitespace合LINQ表達)。 –

+0

更新我的問題與預期的輸出@ S.Petrosov –

回答

3

你可以在你的Any lambda表達式添加&& (string.IsNullOrEmpty(o.MulDivFlg) != string.IsNullOrEmpty(lstExchgMatch[0].MulDivFlg))條件:

var tMisMatchList = lstExchgMatch.Any(o => o.MulDivFlg != lstExchgMatch[0].MulDivFlg && 
    (string.IsNullOrEmpty(o.MulDivFlg) != string.IsNullOrEmpty(lstExchgMatch[0].MulDivFlg))); 
+0

非常感謝你..它的工作! –

+0

@ArpitaDutta歡迎您 – Ian