tags:

views:

49

answers:

1

I have 6 text boxes on an asp page and one search button. Users can search by entering text in any of the search boxes. I am using Linq to query to my sql server database and I can write the queries. but the problem is since there are so many text boxes I will have to write lot of queries. for ex: user can search by entering data in all 6 text boxes or 5 text boxes or 4 or 3 or 2 or 1. You can see how many queries I have to write. Is there any way to code this kind of problem or should I just code all possible combinations.

Thanks

+1  A: 

Generally when i have filtering like yours i just do a null check.

 public List<TABLE> Filters(string a, string b, string c ...){
    var query = (from x in context.TABLE
            where 
                  (string.IsNullOrEmpty(a) || x.A ==a) &&
                  (string.IsNullOrEmpty(b) || x.B ==b) &&
                  (string.IsNullOrEmpty(c) || x.C ==b) 
                 select x
    );

   return query.ToList();
  }

With Joins

   var baseQuery = (from x in context.TABLE
            where 
                  (string.IsNullOrEmpty(a) || x.A ==a) &&
            select x
    );

  if(!string.IsNullOrEmpty(b)){
     baseQuery = (
        from item in baseQuery
        join b in context.Bs on item.JoinMeCode equals b.JoinMeCode
        select item

     );
  }
Nix
seems correct but the problem is I have multiple tables in the database. depending on each textbox I have to write joins.
its still possible, use iqueryable.
Nix
thanks Nix, I got it.