Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:35:10+00:00 2026-05-23T07:35:10+00:00

I have a C# application with a user interface that contains options for the

  • 0

I have a C# application with a user interface that contains options for the type of search a user can perform. The options are ‘multiple terms’ (splits the search term on spaces), ‘case sensitive’, and ‘regular expression’. More options may be added in the future.

The options are stored in the properties IsMultipleTerms, IsCaseSensitive, and IsRegularExpression.

Each combination of options has a different search predicate, and search predicates are defined like so:

private bool SearchCaseInsensitive(string field)
{
    return field.ToLower().Contains(_searchTermLower);
}

private bool SearchCaseInsensitiveMultiple(string field)
{
    return _searchTermsLower.All(field.ToLower().Contains);
}

I filter the list like so:

var predicate = GetFilterPredicate();

SearchResults.Where(predicate);

I currently achieve the lookup by using a class called SearchPredicateOptionSet:

public class PredicateOptionSet
{
    public bool IsCaseSensitive { get; set; }
    public bool IsRegularExpression { get; set; }
    public bool IsMultipleTerms { get; set; }

    public Func<SearchResult, bool> Predicate { get; set; }

    public PredicateOptionSet(bool isCaseSensitive, bool isRegularExpression, bool isMultipleTerms, 
        Func<SearchResult, bool> predicate)
    {
        IsCaseSensitive = isCaseSensitive;
        IsRegularExpression = isRegularExpression;
        IsMultipleTerms = isMultipleTerms;

        Predicate = predicate;
    }
}

I create a list of them and then query it:

private readonly List<PredicateOptionSet> _predicates;

public MainWindow()
{
    _predicates = new List<PredicateOptionSet>
    {
        new PredicateOptionSet(true, false, false, result => Search(result.Name)),
        new PredicateOptionSet(false, false, false, result => SearchCaseInsensitive(result.Name)),

        new PredicateOptionSet(true, false, true, result => SearchMultiple(result.Name)),
        new PredicateOptionSet(false, false, true, result => SearchCaseInsensitiveMultiple(result.Name)),
    };
}

private Func<SearchResult, bool> GetFilterPredicate()
{
    var predicate = from p in _predicates
        where p.IsCaseSensitive == IsCaseSensitive &&
            p.IsMultipleTerms == IsMultipleTerms &&
            p.IsRegularExpression == IsRegularExpression
        select p.Predicate;

    return predicate.First();
}

Is there a cleaner way to achieve this? I feel like I may be missing an important concept.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:35:10+00:00Added an answer on May 23, 2026 at 7:35 am

    At least for the check part you could use an Enum with the [Flags] attribute to create bit field. That might be a little more extensible if you add more methods in the future. You could then use a simple lookup table and do away with the PredicateOptionSet class. Example:

    [Flags]
    public enum PredicateOption
    {
        IsCaseSensitive, IsRegularExpression, IsMultipleTerms
    };
    

    …

    public Dictionary<PredicateOption, Func<SearchResult, bool>> _predicates
        = new Dictionary<PredicateOption, Func<SearchResult, bool>>();
    _predicates.Add(PredicateOption.IsCaseSensitive, result => Search(result.Name));
    _predicates.Add(PredicateOption.IsCaseSensitive | PredicateOption.IsMultipleTerms,
        result => SearchCaseInsensitiveMultiple(result.Name));
    

    ….

    PredicateOption option = PredicateOption.IsCaseSensitive | PredicateOption.IsMultipleTerms;
    SearchResults.Where(_predicates[option]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly large application that uses WPF for its user interface. I
I have to make a graphical user interface application using the language of my
We have a customer that would like to modify application user messages that we
We have an application containing a lot of user controls that update frequently based
I have an application that requires the user to reenter their password between 15
As I said, i have a java desktop application. The interface contains a JList
I am making a application that the user will have to interact with one
I have an console application which interact another user interface. Interface sends commands and
We currently have a quite complex business application that contains a huge lot of
In our current application I have a report that contains something like: if($foo ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.