Only Bolivian Here
Only Bolivian Here

Reputation: 36733

Get the value of a column name when column name is a variable using EF4

Assuming I already have type of shipping I need to find in variables:

var categoryLand = ciudad.CategoriaTierra; //string: "A"
var categoryAir = ciudad.CategoriaAvion; //string "G"

And that I also have the specific instance of the ShippingProducto object I want to use:

EFShippingProductoRepository productRepository = new EFShippingProductoRepository();
int convProductId = Convert.ToInt32(productId);
var product = productRepository.FindProductById(convProductId);

How can I find the value inside the column named categoryLand? For example, if in the code above, categoryLand had the value "C", I need to find the value inside of the column C.

Any suggestions?

enter image description here

Upvotes: 0

Views: 83

Answers (1)

ElDog
ElDog

Reputation: 1377

You can get the property value via reflection:

object value = product.GetType().GetProperty(categoryLand).GetValue(product,

null);

Upvotes: 1

Related Questions