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.
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);
}
}
foreach (BloodBank b in db.BloodBanks.Where(d => where d.bbUserName == Session["username"].ToString())
{
b.col = TextBox1.Text;
}
精彩评论