Iti Tyagi
Iti Tyagi

Reputation: 3661

Using string in place of property name (LINQ)

I have been able to get the values from tables using linq.

var q=(from app in context.Applicant 
        where app.ApplicantName=="")

Now what I want is this:

 var q=(from app in context.Applicant 
        where app.stringIhave =="")  // so instead of column name I have string which has same name as column.

Is it possible to specify string in Select as this is not sure what I will get in each case, I need different data all the time.

Is it possible to do so? If no, then I will figure out something else. Update I have a GlobalString, which holds the column name of a table. So when I query that table, I only specify from string which column value I want to get:

var q=(from app in context.Applicants
       where app.ID==1013
       select GlobalString //which is specifying that I want to get value from which column, as column name is not fixed.
 //where GlobalString can have values like: app.FirstName..app.LastName etc

Update1:

var q = context.Applicants.Select("new(it.ApplicantFirstName as FirstName, it.ApplicantLastName as LastName)");

Error Message:

The query syntax is not valid. Near keyword 'AS'

Upvotes: 0

Views: 133

Answers (2)

Grundy
Grundy

Reputation: 13381

for select you can try something like this

var q = context.Applicant.Select("new(it.FirstName as FirstName, it.LastName as LastName)");

so you only need construct string for that format

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

You can use Dynamic Linq (available from NuGet) for that:

   var q = context.Applicant.Where(app.stringIhave + " = @0", "");

Upvotes: 2

Related Questions