Get field from dynamically/programatically named column name with Entity Framework(使用实体框架从动态/编程命名的列名中获取字段)
问题描述
我正在寻找一种以动态/编程方式更改列和字段名称的方法;
I'm looking for a method to change column and field names dynamically/programatically;
作为:
string iLoadProfileValue = "ColumnName";
string lastCol = DatabaseFunctions.DatabaseClient
.tbl_MeterLoadProfile
.OrderByDescending(a => a.MeterReadDate)
.FirstOrDefault(a => a.MeterID == meterID).iLoadProfileValue;
我将以编程方式更改 iLoadProfileValue 的值.我想将该列的值设为 lastCol 变量.
I'll change iLoadProfileValue's value programatically. And I would like to get that column's value to lastCol variable.
怎么做?
非常感谢.
完成:
最后的情况是这样的:感谢 thepirat000 和 Dismissile
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);
}
推荐答案
您可以使用反射来获取属性列表.查看 System.Type 上的 GetProperties() 方法.
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()
然后您可以使用 LINQ 查找与您想要的属性匹配的属性:
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);
}
正如 thepirat000 在评论中指出的那样,如果您只关心单个属性,您可以调用方法 GetProperty(string name) 而不是 GetProperties().如果您只关心一个属性,而不是反映实体中的所有列,这可能会更有效.
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.
这篇关于使用实体框架从动态/编程命名的列名中获取字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用实体框架从动态/编程命名的列名中获取字段
				
        
 
            
        基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
 - 首先创建代码,多对多,关联表中的附加字段 2022-01-01
 - 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
 - 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
 - 错误“此流不支持搜索操作"在 C# 中 2022-01-01
 - 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
 - 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
 - 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
 - JSON.NET 中基于属性的类型解析 2022-01-01
 - 如何动态获取文本框中datagridview列的总和 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				