0

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'
Incredible
  • 3,495
  • 8
  • 49
  • 77

2 Answers2

2

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

   var q = context.Applicant.Where(app.stringIhave + " = @0", "");
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

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

Grundy
  • 13,356
  • 3
  • 35
  • 55