Pergunta

I would like to get "Column Name, Column's Value" in an tuple array list with entity framework.

I have this:

var colNames = typeof(tbl_ObisSchema).GetProperties().Select(a => a.Name).ToList();

to get all column names

 using (var dbContext = new db_ReadyEngine_MSSQL())
{
 var colNames = typeof(tbl_ObisSchema).GetProperties().Select(a => a.Name).ToList();

 List<Tuple<string, string>> list = new List<Tuple<string, string>>();
 list.Add(Tuple.Create(ColumnName, ColumnValue));

}

How done it? Thanks a lot...

Foi útil?

Solução

A column on its own isn't enough; you'll need to read the column values from a specific entity.

Once you have an entity, you can use the DbContext.Entry method to get the DbEntityEntry instance for that entity. The CurrentValues property will then give you access to the current property values for that entity.

using (var dbContext = new db_ReadyEngine_MSSQL())
{
    var entity = dbContext.Set<tbl_ObisSchema>().Find(thePrimaryKeyToFind);
    if (entity == null) throw new InvalidOperationException("Record not found");

    var entry = dbContext.Entry(entity);
    var currentPropertyValues = entry.CurrentValues;

    List<Tuple<string, object>> list = currentPropertyValues.PropertyNames
        .Select(name => Tuple.Create(name, currentPropertyValues[name]))
        .ToList();

    // Do something with the list...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top