0

実行時の値に基づいて更新するプロパティの 1 つを選択する必要があるシナリオがあります。

Person
PersonId
PeriodAge1
PeriodAge2
PeriodAge3
..
Period50

int currentPeriod = GetCurrentPeriodFromWhereEver();
Person p = Context.Persons.where(p=>p.PersonId=="Doe").firstOrDefault();

if(currentPeriod==1)
p.PeriodAge1 = 10
else if (currentPeriod==2)
p.PeriodAge2 = 112
...
else if (currentPeriod==50)
p.PeriodAge50 = 221

これを行うより良い方法はありますか?エンティティフレームワークで文字列を連結する方法はありますか?これを可能にする何か

string pAge = "PeriodAge";
string cPeriod = "5";
string combinedProperty = pAge + cPeriod; //PeriodAge5

Person p = Context.Persons.where(p=>p.PersonId=="Doe")
.FirstOrDefault()
.Update(p=>combinedProperty = 111);
4

1 に答える 1

2

このようなものを使用できます

string pAge = "PeriodAge";
string cPeriod = "5";
string combinedProperty = pAge + cPeriod; //PeriodAge5

var person = Context.Persons.FirstOrDefault(p => p.PersonId == "Doe");
// The essential part:
Context.Entry(person).Property(combinedProperty).CurrentValue = 111;
于 2016-02-15T14:59:05.347 に答える