3

How to ignore NULL and empty string match while performing the below query?

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

For example, the list has below 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 above query is returning true. since "" and NULL mismatch has occurred. But my expectation is to ignore null and "" comparison check. It would perform not nullable and non empty string match only. It should ignore the presence of null and "" and treat both as equal

The above query is returning false as it is considering null is not equal to "". But I want it to return true and consider null and "" as equal or ignore wheneever the string is null or "".

Arpita Dutta
  • 291
  • 7
  • 19

1 Answers1

3

You could add && (string.IsNullOrEmpty(o.MulDivFlg) != string.IsNullOrEmpty(lstExchgMatch[0].MulDivFlg)) condition in your Any lambda expression:

var tMisMatchList = lstExchgMatch.Any(o => o.MulDivFlg != lstExchgMatch[0].MulDivFlg &&
    (string.IsNullOrEmpty(o.MulDivFlg) != string.IsNullOrEmpty(lstExchgMatch[0].MulDivFlg))); 
Ian
  • 30,182
  • 19
  • 69
  • 107