Get field from dynamically/programatically named column name with Entity Framework

10,940

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.

Share:
10,940
Erçin Dedeoğlu
Author by

Erçin Dedeoğlu

I am passionate about solving problems and developing products in order to make people's lives easier. I am digging devices with more than 16 years’ experience producing code to a consistently high standard. I am constantly honing my skills within the web development arena, and apply my knowledge to implement a range of practical solutions with the utmost efficiency. I possess a proven track record of developing .NET applications and am taking a serious role that will utilize and expand on these skills. Erçin - Online Resume Linkedin

Updated on June 27, 2022

Comments

  • Erçin Dedeoğlu
    Erçin Dedeoğlu almost 2 years

    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);
    }
    
  • Erçin Dedeoğlu
    Erçin Dedeoğlu about 10 years
    Thanks for the fast respond. prntscr.com/30ej8f I tried and got like this. How can I dinamically select values with variable?
  • Dismissile
    Dismissile about 10 years
    PropertyInfo has a method called GetValue(object) which takes as its parameter the instance that you want to get the value for. I edited the post to show this. msdn.microsoft.com/en-us/library/hh194385(v=vs.110).aspx
  • thepirat000
    thepirat000 about 10 years
    You probably don't need to get all the properties with GetProperties(), you can just call GetProperty(iLoadProfileValue) to obtain the property.
  • Dismissile
    Dismissile about 10 years
    @thepirat000 Great suggestion. If he wants to get all of the properties in a loop it probably makes sense to use GetProperties, otherwise if you only need a single one then GetProperty makes a lot more sense.
  • Erçin Dedeoğlu
    Erçin Dedeoğlu about 10 years
    Thanks a lot both of you
  • Erçin Dedeoğlu
    Erçin Dedeoğlu about 10 years
    Could you look to stackoverflow.com/questions/22383198/… please?