Goldentp
Goldentp

Reputation: 197

Dynamic Linq with a database

I am trying to understand dynamic linq as used by Scott Guthrie in this example Dynamic Query

Scott attached a file as an example in the CsharpSamples file where he uses a dynamic query to look in multiple columns for a search. I am trying to understand how a dynamic query works so I may adapt his code to work in my program.

This code snippet below is giving me the most trouble

var query = db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
            OrderBy("CompanyName").
            Select("New(CompanyName as Name, Phone)");

I am not understanding why on the first line he hard codes the value "London and "10"?

I want to use dynamic Query for a search function where the table is called iamp_mapping and the columns I would like to search are called PA, Major Program, Investment_Area, Director and VP.

I want to the user to be able to use a single search box to search every column for the existence of a value. Also here is the search function I would like to use to search for the value. I was able to get it working with a single column search but not with every column.

if (!String.IsNullOrEmpty(searchString))
{
}

I hope that this makes sense, please feel free to ask any questions that would help diagnose my problem. I will be checking back often.

Upvotes: 0

Views: 145

Answers (1)

Styxxy
Styxxy

Reputation: 7517

As I understand from your comment, you would like something like this:

var query = db.iamp_mapping.Where("PA == %0 and Major_Program == %0 and Investment_Area == %0 and Director == @0 and VP == @0", "ValueToSearchFor").
            OrderBy("...").
            Select("...");

Note: I didn't test it exactly, but from my intuition, I would say this works. Of course you still have to add the OrderBy and Select correctly.

The @0 is just the first value parameter you give the Where method.

Upvotes: 1

Related Questions