Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamically adding filters to a string.Contains

I’m building a small tool to help me filter data in a massive logfile (about 4.5 million lines).
In order to filter these results, I’m using searchparameters which should either be included in the search or excluded from the search results.

I’m reading the logfile line by line due to memory space restrictions.
For each line I’m checking if the neccessary conditions are met.
So it looks something like this:

if (line.Contains(parameterToInclude1) && line.Contains(parameterToInclude2) && !line.Contains(parameterToExclude1) && !line.Contains(parameterToExclude2))

Hard-coded it works fine, however I’m trying to make this dynamic by allowing the user to add parameters to include and exclude.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For this I’m using a class called SearchParametersClass

public class SearchParametersClass
{
    public List<string> included { get; set; }
    public List<string> excluded { get; set; }
}

The question is now, how can I make my code so it checks if the line contains the different parameters from the Included list and excludes the parameters from the Excluded list?

Thanks

>Solution :

A simple (but maybe not the fastest) way to do it with Linq would look like this:

if (searchParams.included.All(x => line.Contains(x)) && 
  searchParams.excluded.All(x => !line.Contains(x)))
{
...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading