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 8693863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:38:30+00:00 2026-06-13T00:38:30+00:00

I have a gridview which has drop down boxes in each header for filtering.

  • 0

I have a gridview which has drop down boxes in each header for filtering. Each filter is loaded with the distinct values from its column when loaded. At run time, I add “ALL” to allow the user to select all from that field. I am trying to build the linq statement dynamically to ignore the field if the drop down box is set to “ALL”. Is this possible? I want to see if I can do this in one single statement. The example below only shows 2 dropdown boxes, but my actually case has up to 5.

If I choose to use if then statements, I end up with spaghetti code.

DropDownList drpOwners = this.grdOtherQuotes.HeaderRow.FindControl("drpOwners") as DropDownList;                     
DropDownList drpCompanyName = this.grdOtherQuotes.HeaderRow.FindControl("drpCompanyName") as DropDownList;

var filteredList = (from x in allQuotes
                          where (drpOwners.SelectedValue != ALL) ? x.SalesRepFullName == drpOwners.SelectedValue:true                                    
                                && drpCompanyName.SelectedValue != ALL ? x.CompanyName == drpCompanyName.SelectedValue: true
                          select x);
  • 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-06-13T00:38:32+00:00Added an answer on June 13, 2026 at 12:38 am

    Personally, I’d find having this broken up to be simpler:

    IEnumerable<Quote> filteredList = allQuotes;
    // If using EF or LINQ to SQL, use: IQueryable<Quote> filteredList = allQuotes;
    if (drpOwners.SelectedValue != ALL) 
       filteredList = filteredList.Where(x => x.SalesRepFullName == drpOwners.SelectedValue);
    if (drpCompanyName.SelectedValue != ALL) 
       filteredList = filteredList.Where(x => x.CompanyName == drpCompanyName.SelectedValue);
    // More conditions as needed
    

    This really isn’t any longer, and it’s far simpler to follow.


    If you really wanted to be able to write this as a “one-liner”, you could make an extension method to build the query. For example, if using Entity Framework:

    static IQueryable<T> AddCondition(this IQueryable<T> queryable, Func<bool> predicate, Expression<Func<T,bool>> filter)
    {
         if (predicate())
             return queryable.Where(filter);
         else
             return queryable;
    }
    

    This would then let you write this as:

    var filteredList = allQuotes
                         .AddCondition(() => drpOwners.SelectedValue != ALL, x => x.SalesRepFullName == drpOwners.SelectedValue)
                         .AddCondition(() => drpCompanyName.SelectedValue != ALL, x.CompanyName == drpCompanyName.SelectedValue);
    

    You could, of course, take this even further, and make a version that hard-wires the predicate to check a combo box against “ALL”, making the predicate shorter (just the combo box).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView in which each row has a custom view. The grid
I have gridview which contains 60 rows and each row has 4 radiobutton option
In my website, I have a gridview which has datasource dynamically bound from database
I have a devexpress gridview within my asp.net page which has a filter row.
For example, Lets say that I have a gridview column which has important controls,
I have a GridView on a .aspx page with a hidden column which has
ok i have a project which has many gridview in its pages... now i
I have a gridview within an updatepanel which allows paging and has a linkbutton
I have a GridView which is programatically filled from the DB (not a SqlDataSource
I have a gridview for which JQuery drag and drop method is used to

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.