Why does this linq query not give me expected result?

101 views Asked by At

The following query doesn't give me the expected result. What I am expecting is I need all contacts if any of the conditions are matched, but it doesn't give me that result

 Func<BAL.Contact, bool> expr_contact =
            x => x.Name.NullSafeStartWith(txtSearch.Text)
          || x.ContactDetails.All(a => a.TP.StartsWith(txtSearch.Text));

I searched for contact name, but if the searchtext does not match the contact details, then I get an empty result :(

1

There are 1 answers

0
Simon Whitehead On BEST ANSWER

You're saying "Name starts with X.. OR ALL Contact Details start with X". You want "Name starts with X .. Or ANY Contact Details start with X":

Func<BAL.Contact, bool> expr_contact =
        x => x.Name.NullSafeStartWith(txtSearch.Text)
      || x.ContactDetails.Any(a => a.TP.StartsWith(txtSearch.Text));
//                        ^^^ Any