none
Linq query with variable parameters and wildcards RRS feed

  • Question

  • Hi,

    I'm currently working on a project that needs to filter out results of bookings by different conditions. I'm not really happy about the code that I've produced because it feels like it can be done in a nicer way but I don't know how.

    I've got a list of bookings, that contain a object holding the customer data as well as dates and some other information. I need to offer filters for the first and last name, the user id and the date for which the booking has been made. Wildcards are allowed in the query.

    Here's the code that applies all these filters successively if the need to be set. I don't like that I have to differentiate between all these StartsWith, EndsWith and Cobntains.

     

    private
     void
     ApplyFilters()
            {
                var
     resultSet = resmanager.GetReservations();
    
                //Filter by last name
    
                if
     (this
    .tbLastName.Text != string
    .Empty)
                {
                    string
     lastNameFilter = this
    .tbLastName.Text.ToLower();
    
                    if
     (lastNameFilter.StartsWith(WILDCARD_CHAR.ToString()) && lastNameFilter.EndsWith(WILDCARD_CHAR.ToString()))
                    {
                        lastNameFilter = lastNameFilter.Trim(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.LastName.ToLower().Contains(lastNameFilter)
                                    select
     reservation;
                    }
                    else
     if
     (lastNameFilter.StartsWith(WILDCARD_CHAR.ToString()))
                    {
                        lastNameFilter = lastNameFilter.TrimStart(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.LastName.ToLower().EndsWith(lastNameFilter)
                                    select
     reservation;
                    }
                    else
     if
     (lastNameFilter.EndsWith(WILDCARD_CHAR.ToString()))
                    {
                        lastNameFilter = lastNameFilter.TrimEnd(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.LastName.ToLower().StartsWith(lastNameFilter)
                                    select
     reservation;
                    }
                    else
    
                    {
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.LastName.ToLower() == lastNameFilter
                                    select
     reservation;
                    }
                }
    
                //Filter by first name
    
                if
     (this
    .tbFirstName.Text != string
    .Empty)
                {
                    string
     firstNameFilter = this
    .tbFirstName.Text;
    
                    if
     (firstNameFilter.StartsWith(WILDCARD_CHAR.ToString()) && firstNameFilter.EndsWith(WILDCARD_CHAR.ToString()))
                    {
                        firstNameFilter = firstNameFilter.Trim(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.FirstName.ToLower().Contains(firstNameFilter)
                                    select
     reservation;
                    }
                    else
     if
     (firstNameFilter.StartsWith(WILDCARD_CHAR.ToString()))
                    {
                        firstNameFilter = firstNameFilter.TrimStart(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.FirstName.ToLower().EndsWith(firstNameFilter)
                                    select
     reservation;
                    }
                    else
     if
     (firstNameFilter.EndsWith(WILDCARD_CHAR.ToString()))
                    {
                        firstNameFilter = firstNameFilter.TrimEnd(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.FirstName.ToLower().StartsWith(firstNameFilter)
                                    select
     reservation;
                    }
                    else
    
                    {
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.FirstName.ToLower() == firstNameFilter
                                    select
     reservation;
                    }
                }
    
                //Filter by socrat id
    
                if
     (this
    .tbSocratId.Text != string
    .Empty)
                {
                    string
     socratIdFilter = this
    .tbSocratId.Text;
    
                    if
     (socratIdFilter.StartsWith(WILDCARD_CHAR.ToString()) && socratIdFilter.EndsWith(WILDCARD_CHAR.ToString()))
                    {
                        socratIdFilter = socratIdFilter.Trim(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.SocratId.ToLower().Contains(socratIdFilter)
                                    select
     reservation;
                    }
                    else
     if
     (socratIdFilter.StartsWith(WILDCARD_CHAR.ToString()))
                    {
                        socratIdFilter = socratIdFilter.TrimStart(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.SocratId.ToLower().EndsWith(socratIdFilter)
                                    select
     reservation;
                    }
                    else
     if
     (socratIdFilter.EndsWith(WILDCARD_CHAR.ToString()))
                    {
                        socratIdFilter = socratIdFilter.TrimEnd(new
     char
    [] { WILDCARD_CHAR });
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.SocratId.ToLower().StartsWith(socratIdFilter)
                                    select
     reservation;
                    }
                    else
    
                    {
                        resultSet = from
     reservation in
     resultSet
                                    where
     reservation.Student.SocratId.ToLower() == socratIdFilter
                                    select
     reservation;
                    }
                }
    
                //Filter by date for which meal has been booked
    
                if
     (this
    .datePicker.SelectedDate.HasValue)
                {
                    DateTime selectedDate = this
    .datePicker.SelectedDate.Value;
                    resultSet = from
     reservation in
     resultSet
                                where
     reservation.BookedMealDate.Date == selectedDate.Date
                                select
     reservation;
                }
    
                this
    .menuReservationDataGrid.ItemsSource = resultSet;
            }
    

    can anybody point me top a 'nicer' way of doing this?

     

    Thanks, Philippe

    Monday, March 22, 2010 8:36 AM

Answers