Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework ASP.NET MVC中动态类型中的动态字段名_Entity Framework_C# 4.0 - Fatal编程技术网

Entity framework ASP.NET MVC中动态类型中的动态字段名

Entity framework ASP.NET MVC中动态类型中的动态字段名,entity-framework,c#-4.0,Entity Framework,C# 4.0,我使用的是实体框架,我的表结构如下 钥匙,2000年12月,2001年12月,2002年12月,。。。2020年12月 因此,在循环中更新if时,对于中的所有列,我必须指定列名,如 for (int j = 0; j < intEndYear - intStartYear; j++) { // Find value from FormCollection // Assign it to object OBJECT.DEC2000 = 1; } 所以,在更新时,我试着像 //

我使用的是实体框架,我的表结构如下

钥匙,2000年12月,2001年12月,2002年12月,。。。2020年12月

因此,在循环中更新if时,对于中的所有列,我必须指定列名,如

for (int j = 0; j < intEndYear - intStartYear; j++)
{
  // Find value from FormCollection
  // Assign it to object
  OBJECT.DEC2000 = 1;
}
所以,在更新时,我试着像

// trying to make field name
string stryear = "DEC" + year.ToString(); 
objLevel.stryear = 1; // should read objLevel.dec2000 = 1;
我知道这不起作用,因为我的表中没有strYear列

问:有没有其他好办法来处理这种情况?因此,我不必检查每一列,而是可以使用dynamic/

感谢您的指导。

您可以使用。有关更多信息,请参阅

//assuming "myInstance" is an instance of the EF class:
string strYear = "DEC"+year.ToString();
var pi =myInstance.GetType().GetProperty(styYear);
if (pi!=null)
{
  pi.SetValue(MyInstance,1);
}

啊,我完全错过了反射部分。谢谢,它工作得很好。
//assuming "myInstance" is an instance of the EF class:
string strYear = "DEC"+year.ToString();
var pi =myInstance.GetType().GetProperty(styYear);
if (pi!=null)
{
  pi.SetValue(MyInstance,1);
}