If you ever need to retrieve data from EF using the name of a column, rather than the built in Intellisense, then the following vb.net code snippet may help.

GetType(TypeOfEntity).GetProperty(NameOfColumn).GetValue(ActualEntity, Nothing)

  • TypeOfEntity is the name of the Entity from which you wish to retrieve data.
  • NameOfColumn is obviously the Name of the Column from which you wish to retrieve data.
  • ActualEntity is a reference to the Entity itself, from which the data is to be retrieved.

The above can help if you need to retrieve data based on a column name which is defined by another function. Such as in the following example;

You have a table called Users with columns called Screen1Edit and Screen2Edit.

You can then retrieve the values in Screen1Edit or Screen2Edit depending upon which you require, supplied in a variable called strScreenName;

GetType(Users).GetProperty(strScreenName).GetValue(Users, Nothing)

Obviously you must be careful to supply the exact name of the column into the Column Name!

Note: For c# replace GetType with TypeOf.

By |2017-07-24T08:33:19+01:00November 20th, 2012|.Net Framework, C#, Data, Entity Framework, VB.net, Visual Studio|0 Comments

About the Author:

Leave A Comment