Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - how do I get the columns?

I wish to get a list of columns names, types and whether the column is a PK of a table object in Entity Framework.

How do I do this in C# (4.0) (ideally generically)?

The winning answer will be one that does it efficiently and most importantly generically.

like image 802
Dan B Avatar asked May 19 '11 09:05

Dan B


People also ask

How do I get specific columns in Entity Framework?

In Entity Framework Core we can select specific columns by using either anonymous types or DTOs.

How do I get column names in Entity Framework?

var names = typeof(User). GetProperties() . Select(property => property.Name) . ToArray();

How do I get a single column value in Entity Framework?

You could use the LINQ select clause and reference the property that relates to your Name column. Show activity on this post. If you're fetching a single item only then, you need use select before your FirstOrDefault()/SingleOrDefault(). And you can use anonymous object of the required properties.


2 Answers

if you want only column names then ,i got the best answer :
var properties = (from t in typeof(YourTableName).GetProperties() select t.Name).ToList(); var name= properties[0];

like image 30
Ashitosh birajdar Avatar answered Oct 22 '22 22:10

Ashitosh birajdar


Got it - I used a linq based reflection query:

IEnumerable<FieldList> properties = from p in typeof(T).GetProperties()
                                    where (from a in p.GetCustomAttributes(false)
                                    where a is EdmScalarPropertyAttribute   
                                    select true).FirstOrDefault()

Sorted! Thanks for the suggestions all.

FYI - I am creating a dynamic where clause using LINQ, dynamic lambda expressions to build e.g. search which will automatically search through all columns by default. But I also needed the column names to verify because I will allow this to be overridden and these calls will be done via javascript ajax post whose input cannot be trusted - so needed to verify the column names.

I used the above to place the results into a custom object with properties called FieldName, FieldType, PrimaryKey. Ta daaa.

Customise it further with

IEnumerable<FieldList> properties = from p in typeof(T).GetProperties()
                                    where (from a in p.GetCustomAttributes(false)
                                    where a is EdmScalarPropertyAttribute
                                    select true).FirstOrDefault()
                                    select new FieldList
                                    {
                                       FieldName = p.Name,
                                       FieldType = p.PropertyType,
                                       FieldPK = p.GetCustomAttributes(false).Where(a => a is EdmScalarPropertyAttribute && ((EdmScalarPropertyAttribute)a).EntityKeyProperty).Count() > 0
                                     };    
like image 65
Dan B Avatar answered Oct 22 '22 22:10

Dan B