How to select a column name using Linq

Viewed 215

I am stuck in a situation where my logic needs some improvement, which i am trying to do. The situation is we all know how to update the specific detail of column like this.

Person result = (from p in Context.Persons
          where p.person_id == 5
          select p).SingleOrDefault();

result.is_default = false;

Context.SaveChanges();

Okay, all good !

Now let's say I have a value in session is_default i.e the column name of the table.

How can I update the column using that value like so:

update table name set "Session value" = true 

Here the session value is is_default which is the column name of the table. Is this possible using EF , LINQ .

1 Answers

you can use the concept of reflection for this as given below

Person result = (from p in Context.Persons
          where p.person_id == 5
          select p).SingleOrDefault();

PropertyInfo propertyInfo = result.GetType().GetProperty("Session value");
propertyInfo.SetValue(result , true, null);
Related