Ignore null and empty string in Linq Query
How can I ignore NULL and empty string matches when doing the following query?
var tMisMatchList = lstExchgMatch.Any(o =>
o.MulDivFlg != lstExchgMatch[0].MulDivFlg);
For example, a list has the following data
[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 = "";
In this case, my previous query returns true. since there was a mismatch between "and NULL. But my expectation is to ignore the comparison check" null "and" ". It will only do non-empty and non-empty strings. It should ignore the presence of null and" "and treat them as equal
The above query returns false because it thinks null is not equal to "". But I want it to return true and consider null and "equal or ignore when the string was null or" ".
source to share
You can add a condition && (string.IsNullOrEmpty(o.MulDivFlg) != string.IsNullOrEmpty(lstExchgMatch[0].MulDivFlg))
in your lambda expression Any
:
var tMisMatchList = lstExchgMatch.Any(o => o.MulDivFlg != lstExchgMatch[0].MulDivFlg &&
(string.IsNullOrEmpty(o.MulDivFlg) != string.IsNullOrEmpty(lstExchgMatch[0].MulDivFlg)));
source to share