Reputation: 228
I am using Entity Framework and my table structure is as below.
Key, dec2000, dec2001, dec2002,... dec2020
so, while update in loop if for all columns in i have to specify the column name like,
for (int j = 0; j < intEndYear - intStartYear; j++)
{
// Find value from FormCollection
// Assign it to object
OBJECT.DEC2000 = 1;
}
so, one way is i have to check for year and make condition check whether year = 2000 then use dec2000 field and so on.
So i tried to use Dynamic type so i can specify field name dynamically like
dynamic objLevel = objDetailSum.GetSingle(parameters);
so, while updating i am trying to do like
// trying to make field name
string stryear = "DEC" + year.ToString();
objLevel.stryear = 1; // should read objLevel.dec2000 = 1;
I know this will not work as i don't have strYear column in my table.
Question : Is there any other good way to handle this situation? so i don't have to check for each and every column and can use dynamic instead/.
Appreciate any direction.
Upvotes: 1
Views: 1507
Reputation: 7495
You could use Reflection. See here for more information
//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);
}
Upvotes: 2