Come rimanere ASCIUTTO mentre si usa LINQ per Entità e metodi di supporto?

Diciamo che ho un modo particolare di decidere se alcune stringhe "corrispondono", come questo:

public bool stringsMatch(string searchFor, string searchIn)
{
  if (string.IsNullOrEmpty(searchFor))
  {
    return true;
  }

  return searchIn != null &&
    (searchIn.Trim().ToLower().StartsWith(searchFor.Trim().ToLower()) ||
     searchIn.Contains(" " + searchFor));
}

Vorrei estrarre le corrispondenze da un database utilizzando Linq To Entities e questo helper. Tuttavia, quando provo questo:

IQueryable blahs = query.Where(b => stringsMatch(searchText, b.Name);

ottengo "LINQ to Entities non riconosce il metodo ..."

Se riscrivo il codice come:

IQueryable blahs = query.Where(b =>
      string.IsNullOrEmpty(searchText) ||
      (b.Name != null &&
        (b.Name.Trim().ToLower().StartsWith(searchText.Trim().ToLower()) ||
         b.Name.Contains(" " + searchText)));

Che è logicamente equivalente, allora le cose funzionano bene. Il problema è che il codice non è così leggibile e devo riscriverlo per ogni diversa entità che voglio abbinare.

Per quanto posso dire da domande come questa , quello che voglio fare è impossibile al momento, ma spero che mi manchi qualcosa, vero?

8
задан Community 23 May 2017 в 09:59
поделиться