我正在尋找一種方法來動態/以編程方式更改列名稱和字段名稱;使用實體框架從動態/編程命名的列名稱獲取字段
爲:
string iLoadProfileValue = "ColumnName";
string lastCol = DatabaseFunctions.DatabaseClient
.tbl_MeterLoadProfile
.OrderByDescending(a => a.MeterReadDate)
.FirstOrDefault(a => a.MeterID == meterID).iLoadProfileValue;
我會編程修改iLoadProfileValue的價值。 我想獲得該列的值爲lastCol變量。
怎麼辦?
非常感謝。
完成:
最後一種情況是這樣的: 多虧了thepirat000和Dismissile
string iLoadProfileValue = "MeterReadDate";
var myEntity = DatabaseFunctions.DatabaseClient.tbl_MeterLoadProfile.OrderByDescending(a => a.MeterReadDate).FirstOrDefault(a => a.MeterID == 6);
if (myEntity != null)
{
var properties = myEntity.GetType().GetProperty(iLoadProfileValue);
object value = properties.GetValue(myEntity);
}
爲什麼在_iLoadProfileValue_?上使用_i_前綴。只是問問。 – thepirat000
只是一種習慣... –
這是一個壞習慣:)。從_i_開始,意味着該變量是一個int。 – thepirat000