2

I'm creating a query to search the db using EF. TdsDb being the EF context.

string searchValue = "Widget";
TdsDb tdsDb = new TdsDb();
IQueryable<Counterparty> counterparties;

I can do exact match:

counterparties = tdsDb.Counterparties.Where(x => x.CounterpartyName == searchValue);

or wildcard match:

counterparties = tdsDb.Counterparties.Where(x => x.CounterpartyName.Contains(searchValue));

But I want to be able to do both i.e. (psudo code)

counterparties = tdsDb.Counterparties.Where(x => 
          if (searchValue.EndsWith("%")) 
                 {
                      if (searchValue.StartsWith("%"))
                          {x.CounterpartyName.Contains(searchValue)}
                      else 
                          {x.CounterpartyName.StartsWith(searchValue)}
                 }   
          else
                 {x => x.CounterpartyName == searchValue}
      );

Now clearly I can't put an if statement in the where clause like that. But I also can't duplicate the queries: shown here they are hugely dumbed down. The production query is far longer, so having multiple versions of the same long query that vary on only one clause seems very unhealthy and unmaintainable.

Any ideas?

Slauma
  • 175,098
  • 59
  • 401
  • 420
Matt
  • 97
  • 1
  • 6

1 Answers1

1

You should be able to use the ternary operator:

bool startsWithWildCard = searchValue.StartsWith("%");
bool endsWithWildCard = searchValue.EndsWith("%");

counterparties = tdsDb.Counterparties.Where(x => 
      endsWithWildCard
          ? (startsWithWildCard
              ?  x.CounterpartyName.Contains(searchValue)
              : (x.CounterpartyName.StartsWith(searchValue)))
          : (x.CounterpartyName == searchValue));

Did you test btw if querying by a searchValue that has an % at the beginning or end works as you expect? It might be possible that % will be escaped as a character to query for because StartsWith and Contains will prepend/append % wildcards to the generated SQL search term anyway. In that case you need to cut off the % from the searchValue before you pass it into StartsWith or Contains.

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • 1
    Ternaries, will they ever not be the Best Thing Ever? Thanks Slauma, that works and yep, I had allowed for the removal of the wildcards. Very, very useful, thanks very much. – Matt Sep 21 '12 at 09:17