Tuesday, 14 October 2014

Getting Table Column names in Linq to Entity

We can get the list of column names of a table through SQL query using the following query:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'TableName'

To get the same result while using LINQ to Entity Framework, we need to use Reflection.

using (var context = new DatabaseContext())
    var queryString = typeof(TableName).GetProperties().Select(a => a.Name).ToList();                                     
}

No comments: