Simplify IQueryable Linq to SQL query Below I present to you an IQueryable LINQ to SQL query (used with NHibernate).
I have tried to simplify this query but I could not do it.
I want to run a query for searching information.

Options:


search on source or target
search with match case or not
search match exact or not


The query it seems to work but there is much if/else branching.

Is it possible to simplify it?

        if (concordanceSearchOptions.SearchInSource)
        {
            if (concordanceSearchOptions.MatchCase)
            {
                if (concordanceSearchOptions.MatchExact)
                {
                    getByQueryable = getByQueryable.Where(p =&gt; p.SourceSegment.Equals(concordanceSearchOptions.SearchText));
                }
                else
                {
                    getByQueryable = getByQueryable.Where(p =&gt; p.SourceSegment.Contains(concordanceSearchOptions.SearchText));
                }
            }
            else
            {
                if (concordanceSearchOptions.MatchExact)
                {
                    getByQueryable = getByQueryable.Where(p =&gt; p.SourceSegment.ToLower().Equals(concordanceSearchOptions.SearchText.ToLower()));
                }
                else
                {
                    getByQueryable = getByQueryable.Where(p =&gt; p.SourceSegment.ToLower().Contains(concordanceSearchOptions.SearchText.ToLower()));
                }
            }
        }
        else
        {
            if (concordanceSearchOptions.MatchCase)
            {
                if (concordanceSearchOptions.MatchExact)
                {
                    getByQueryable = getByQueryable.Where(p =&gt; p.TargetSegment.Equals(concordanceSearchOptions.SearchText));
                }
                else
                {
                    getByQueryable = getByQueryable.Where(p =&gt; p.TargetSegment.Contains(concordanceSearchOptions.SearchText));
                }
            }
            else
            {
                if (concordanceSearchOptions.MatchExact)
                {
                    getByQueryable = getByQueryable.Where(p =&gt; p.TargetSegment.ToLower().Equals(concordanceSearchOptions.SearchText.ToLower()));
                }
                else
                {
                    getByQueryable = getByQueryable.Where(p =&gt; p.TargetSegment.ToLower().Contains(concordanceSearchOptions.SearchText.ToLower()));
                }
            }
        }
Naming  

Based on the naming guidlines variables/fields should be named "using a noun, noun phrase, or adjective". So getByQuerable should be renamed to something following the guidlines and also meaningful.

Optimization  


you should extract the searching for Source and Target to separate methods. This helps for readability and makes it easier for Mr.Maintainer in a couple of months.  
preparing the searchtext before the actual search takes place will speed up the query.
Right now if concordanceSearchOptions.MatchCase == false you call for each source/target in the where part the ToLower() method of the SearchText property.  


Refactoring  

    String searchText = concordanceSearchOptions.SearchText;
    if (concordanceSearchOptions.MatchCase)
    {
        searchText = searchText.ToLower();
    }

    if (concordanceSearchOptions.SearchInSource)
    { 
        getByQueryable  = GetBySource(concordanceSearchOptions, searchText);
    }
    else
    {
        getByQueryable  = GetByTarget(concordanceSearchOptions, searchText);
    }


private IEnumerable&lt;T&gt; GetBySource(SearchOptions concordanceSearchOptions,String searchText){

}
private IEnumerable&lt;T&gt; GetByTarget(SearchOptions concordanceSearchOptions,String searchText){

}