Search code examples
c#asp.netentity-frameworklinq-to-entities

Get All Columns Names in the selected table provided the class names are passed as a String using Entity Framework 4.0


I want to get the name of all the columns in the selected database provided the condition that the name of the tables whose column to be fetched are in the string array and can be changed on fly.

I have a code where I can get the name of all column of a particular table only:

 var properties = from p in typeof(EntityAttribute).GetProperties()
                                                    where (from a in p.GetCustomAttributes(false)
                                                           where a is EdmScalarPropertyAttribute
                                                           select true).FirstOrDefault()
                                                    select new 
                                                    {
                                                        FieldName = p.Name,
                                                        FieldType = p.PropertyType,
                                                        FieldPK = p.GetCustomAttributes(false).Where(a => a is EdmScalarPropertyAttribute && ((EdmScalarPropertyAttribute)a).EntityKeyProperty).Count() > 0
                                                    };

EntityAttribute is the name of the column.

The problem is that this class name EntityAttribute is a class. i want to pass this class name from the string array.

Example: I have a string array that have the name of say three tables:

Now i want to iterate the code given above by passing the Class Name that is in a string

How should I pass it?


Solution

  • You can use GetType

    Type type = Type.GetType("YourApplication.Namespace.EntityAttribute");