2017-05-10 99 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