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 "".

3
  • Please add what is the result now and what result you expect to receive Commented May 10, 2017 at 7:05
  • see this. Commented May 10, 2017 at 7:07
  • Updated my question with the expected output @S.Petrosov Commented May 10, 2017 at 7:14

1 Answer 1

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))); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.