I
I
INTERNALINTERFERENCE2021-10-13 17:47:38
C++ / C#
INTERNALINTERFERENCE, 2021-10-13 17:47:38

How to shorten this code?

I have a search in some columns, I wrote it like this:

SearchPredicate = ( lEntry, searchText ) =>
                lEntry.Message.Contains( searchText, StringComparison.OrdinalIgnoreCase ) ||
                lEntry.Sender.Contains( searchText, StringComparison.OrdinalIgnoreCase ) ||
                lEntry.Level.Contains( searchText, StringComparison.OrdinalIgnoreCase );


It seems to me that all this can be reduced, but I do not know how.
How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-10-13
@INTERNALINTERFERENCE

If you're really stubborn, you can do something like this:

new[] { lEntry.Message, lEntry.Sender, lEntry.Level }
  .Any(x => x.Contains(searchText, StringComparsion.OrdinalIgnoreCase)

Well, or else you can write your own method that will search for a substring in any of n strings.
For example, this one:
public static bool IsSubstringOfAny(this string search, params string[] samples) =>
  samples.Any(x=>x.Contains(search, StringComparsion.OrdinalIgnoreCase));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question