从实体框架动态/编程指定的列名获取场 [英] Get field from dynamically/programatically named column name with Entity Framework

查看:205
本文介绍了从实体框架动态/编程指定的列名获取场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找改变的列和字段的名称动态/编程的方法;



为:



 字符串iLoadProfileValue =的ColumnName; 

串lastCol = DatabaseFunctions.DatabaseClient
.tbl_MeterLoadProfile
.OrderByDescending(A => a.MeterReadDate)
.FirstOrDefault(A => a.MeterID = = meterID).iLoadProfileValue;



我会编程的方法修改 iLoadProfileValue 的价值。
和我想获得该字段的数值为 lastCol 变量。



如何能做到?



非常感谢



完成:



最后一种情况是这样的:
感谢为 thepirat000 Dismissile

 字符串iLoadProfileValue =MeterReadDate; 
变种myEntity所= DatabaseFunctions.DatabaseClient.tbl_MeterLoadProfile.OrderByDescending(一个= GT; a.MeterReadDate).FirstOrDefault(一个= GT; a.MeterID == 6);

如果(myEntity所!= NULL)
{
VAR性能= myEntity.GetType()的getProperty(iLoadProfileValue)。
对象值= properties.GetValue(myEntity所);
}


解决方案

您可以使用反射来获得属性的列表。看的GetProperties()上的System.Type方法。



http://msdn.microsoft.com/en-us/library/aky14axb(v = vs.110)的.aspx

 公开的PropertyInfo []的GetProperties()

然后,您可以使用LINQ找到maches一个你想要的属性:

  VAR myEntity所= DatabaseFunctions。 DatabaseClient 
.tbl_MeterLoadProfile
.OrderByDescending(A => a.MeterReadDate)
.FirstOrDefault(A => a.MeterID == meterID);

如果(myEntity所!= NULL){
变种属性= myEntity.GetType()的GetProperties();

//遍历公共属性或查询列表中找到你想要
//在这个例子中,我将刚刚得到的第一个属性的人,并用它来获得的价值:
VAR firstProperty = properties.FirstOrDefault();

//获取的价值,这将是一个对象,所以你可能需要将其强制转换
对象值= firstProperty.GetValue(myEntity所);
}



由于thepirat000在评论中指出的那样,如果你只关心一个单一的属性你可以调用的方法的getProperty(字符串名称)而不是的GetProperties()。这很可能是更有效,如果你只关心一个属性,而你是不是反映了在实体中的所有列。


I'm looking for a method to change column and field names dynamically/programatically;

as:

string iLoadProfileValue = "ColumnName";

string lastCol = DatabaseFunctions.DatabaseClient
.tbl_MeterLoadProfile
.OrderByDescending(a => a.MeterReadDate)
.FirstOrDefault(a => a.MeterID == meterID).iLoadProfileValue;

I'll change iLoadProfileValue's value programatically. And I would like to get that column's value to lastCol variable.

How can it be done?

Thanks a lot.

Done:

Last situation like this: THANKS to thepirat000 and 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);
}

解决方案

You could use reflection to get a list of properties. Look at the GetProperties() method on System.Type.

http://msdn.microsoft.com/en-us/library/aky14axb(v=vs.110).aspx

public PropertyInfo[] GetProperties()

You could then use LINQ to find a property that maches the one you want:

var myEntity = DatabaseFunctions.DatabaseClient
    .tbl_MeterLoadProfile
    .OrderByDescending(a => a.MeterReadDate)
    .FirstOrDefault(a => a.MeterID == meterID);

if(myEntity != null) {
    var properties = myEntity.GetType().GetProperties();

    // iterate through the list of public properties or query to find the one you want
    // for this example I will just get the first property, and use it to get the value:
    var firstProperty = properties.FirstOrDefault();

    // get the value, it will be an object so you might need to cast it
    object value = firstProperty.GetValue(myEntity);
}

As thepirat000 pointed out in the comments, if you only care about a single property you can call the method GetProperty(string name) instead of GetProperties(). This would probably be more efficient if you only care about one property, and you are not reflecting over all columns in your entity.

这篇关于从实体框架动态/编程指定的列名获取场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆