0

This is a small application used on a company intranet by 4 people at the most, and I am new to C# and .NET, so the solution does not have to be grandiose.

I am trying to create a search field for simple .NET C.R.U.D app where the user can pick a category they wish to search from (such as Application Name, or Manager) and then a text-box where they can filter the results based on that field name. The items in the drop down menu are the class field members and I would like for all of them to be searchable. I'm using the Dynamic Linq Library to create a string so that I can pass the column name at run-time but for some reason my queries return no results.

Here is my current query

dr_details = dr_details.Where("@0 == @1",searchType, searchString);

So for instance, searchType & searchString get their values from the query string (We'll say "Manager" and "Joe", respectively) so that the query should be substituted as:

dr_details = dr_details.Where("Manager == Joe");

this gives me no results. However if I hard code the string "Manager == Joe" into the query runs fine.

Any suggestions? This problem would make me yank out my hair if it was long enough! :p

jazzcat
  • 3
  • 3
  • So are you sure `searchType` and `searchString` have the correct value in them? – TheNorthWes Jun 20 '14 at 19:56
  • I am, I see their values in the query string in the browser and I also see their values and type (string) in the debugger. – jazzcat Jun 20 '14 at 20:01
  • Alternatively, dynamic linq is a pretty advanced concept. When you call where on a list it actually returns a query that you can continue to add clauses too. You could just use a switch. – TheNorthWes Jun 20 '14 at 20:02
  • I don't truly want a switch just because there are a lot of column names. However I don't think my coworkers would even search for most of the fields so I might just ask the what the most important search fields for them are and make a case out of those. – jazzcat Jun 20 '14 at 20:09

1 Answers1

-1

You don't necessarily have to use dynamic linq for this if you don't want.

Try something like this:

var query = myList.AsQueryable();

switch(searchType)
{
     case "Manger":
        query = query.where(x=> x.Manager == searchString);
        break;
     case "....":
        break;
     default:
        // No search type match in string... an exception? your call
        break;
}

I find this more intuitive since it "builds" the query as it goes...

Edit:

If that is too painful try following this post

Community
  • 1
  • 1
TheNorthWes
  • 2,661
  • 19
  • 35
  • I think I will go with this and just see what they need in the case statement. – jazzcat Jun 20 '14 at 20:20
  • Follow the edit if you want the best. I know it isn't really the answer but my guess would be that somehow the string that is assembled isn't being assembled properly. Look into that or try building it before hand... – TheNorthWes Jun 20 '14 at 20:27
  • That is what I'm thinking as well. I will check out that post over the weekend. I'm coming from a python mindset and looking at all the template types is giving me a headache right now :) – jazzcat Jun 20 '14 at 20:30