开发者

Dynamically select and update a column value in LINQ resultset

开发者 https://www.devze.com 2023-03-05 12:35 出处:网络
I have a scenario in which there exists a LINQ resultset; I used the following query var stockDetails = from d in db.BloodBanks

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号