Select and change a column value in a LINQ resultset dynamically.

asp.net c# dynamic-linq linq-to-sql

Question

I have a scenario in which there exists a LINQ resultset; I used the following query

var stockDetails = from d in db.BloodBanks
                   where d.bbUserName == Session["username"].ToString()
                   select d;

Now I want to use this resultset and update a column's value. The column is being selected dynamically via a string variable.

The code which I am trying to use is:

foreach (BloodBank b in stockDetails)
            {
                b.<--column name from string variable--> = TextBox1.Text;
            }

Please help me out here as to how do I achieve this.

1
3
5/13/2011 3:04:23 PM

Accepted Answer

You can use reflection to get the field by name like this.

foreach (BloodBank b in stockDetails)
{
    FieldInfo f = typeof(BloodBank).GetField("fieldName");
    if (f != null)
    {
       f.SetValue(b, TextBox1.Text);
    }
}
3
5/13/2011 3:07:46 PM

Popular Answer

foreach (BloodBank b in db.BloodBanks.Where(d => where d.bbUserName == Session["username"].ToString())
{
    b.col = TextBox1.Text;
}


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow