Transform query result to an entity in an array

1

In my program I have an ADO.NET Entity Data Model with a couple of tables linked to my database. What I want is to make a query to bring me the data of a table and be able to access them as if it were an array.
If for example in the entity there is a table that is called Element that has the columns IdElement, Name, Description and Image, my intention is to make a query on that table to bring me an element with a certain Id, which I do with the following code:

int id = 34;    
var entidad = entidad.Elemento.First(x => x.IdElemento == id);

Now I need to be able to access its attributes in the following way:

string nombre = entidad["Nombre"];
string desc = entidad["Descripcion"];
string img = entidad["Imagen"];

To be exact, there would be a variable, "column", that would contain the value of the field that I want to access, so it would be accessed like this:

string valor = entidad[columna];

Any ideas on how to do it?
Thanks.

    
asked by Oscar 18.08.2017 в 13:59
source

3 answers

1

I have managed to do it in the following way:
We have instantiated our ADO.NET Entity Data Model with the name entities . Suppose that, among its entities, we want to access the one with the name Element . From this entity we want to take out the element that has a value of 2. In its Primary Key, there is a string, column , which tells us the property of the element that we want take out.
The code would be the following:

object[] key = { 2 };
valor = typeof(Elemento).GetProperty(columna).GetValue(entidades.Elemento.Find(key));
    
answered by 21.08.2017 / 10:43
source
3

You can use Reflexion to achieve what you want, but you have to keep in mind that the uncontrolled use of Reflexion leads to a big loss of performance. If you have to use it a lot, my recommendation is that you save the definitions of properties in static dictionaries for each type you use.

I leave you an implementation of some functions in which you pass an object and the name of a property and if this property exists in the object and is public, it returns the value of the first property it finds with that name,

public object ObtenerValorPropiedadPorNombre(string nombrePropiedad, object contenedor)
{

    IEnumerable<PropertyInfo> props = contenedor.GetType().GetProperties(BindingFlags.Public).Where(c => c.Name == nombrePropiedad);
    if (props.Count() != 0)
    {
        return props.First().GetValue(contenedor);
    }

    throw new Exception($"La propiedad {nombrePropiedad} no existe en el objeto {contenedor.GetType().Name}");

}
public T ObtenerValorPropiedadPorNombre<T>(string nombrePropiedad, object contenedor) where T : class
{

    IEnumerable<PropertyInfo> props = contenedor.GetType().GetProperties(BindingFlags.Public).Where(c => c.Name == nombrePropiedad);
    if (props.Count() != 0)
    {
        return props.First().GetValue(contenedor) as T;
    }

    throw new Exception($"La propiedad {nombrePropiedad} no existe en el objeto {contenedor.GetType().Name}");

}
    
answered by 21.08.2017 в 09:45
0

Create a dictionary and add each property of the object to the dictionary with its respective name / key / key:

var elemento = eentidad.Elemento.First(x => == id);
Dictionary<string,string> elementoDic = new Dictionary<string,string>() { 
   { "Nombre", elemento.Nombre},
   { "Descripcion", elemento.Descripcion},
   { "Imagen", elemento.Imagen}
};

string nombre = elementoDic["Nombre"]; 
string desc = elementoDic["Descripcion"]; 
string img = elementoDic["Imagen"];

However, using the properties of the object directly would be better.

    
answered by 18.08.2017 в 14:16