0

in my database i have many of tables one of Tables is nambed Company". and its columns names are as follows ("ComName","ComAddress","ComType" ..etc) I have a function which get the columns name from Company table :

public List<string> GetColumnNames(string tablename)
{
    var names = typeof(Company).GetProperties().Select(p => p.Name).ToArray();
    return names.ToList();
}

Until now everything is good but what if i want to get the columns of other tables ? so i have to put the name of the new table (that is given in the function as "tablename") instead of "Company" in this sentence :

var names = typeof(Company).GetProperties().Select(p => p.Name).ToArray();

but it is string so its not working. any idea ?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • This looks as if you have some kind of ORM wrapper running that creates C# Objects for your tables. You probably need to feed your other tables to this mapper as well so it creates objects for you. Maybe you are using EntityFramework? Also: change the `.ToArray();` to `.ToList()` and return names directly – Patrick Artner Dec 09 '17 at 22:30
  • you can also omitt the `(string tablename)` and replace it with `()` - it is not used at all... – Patrick Artner Dec 09 '17 at 22:36
  • i am sorry i dont know what is ORM wrapper .but here is what i am doing: the user can select one of the tables of my database and according to the table the function will show the columns name. and yes i am using EntityFramework – İyad Mansür Dec 09 '17 at 22:37
  • yeah until now its not but i have to use it instad of "Company" in this sentence : var names = typeof(Company).GetProperties().Select(p => p.Name).ToArray(); so it will be like : var names = typeof(tablename).GetProperties().Select(p => p.Name).ToArray(); – İyad Mansür Dec 09 '17 at 22:38
  • [ORM](https://en.wikipedia.org/wiki/List_of_object-relational_mapping_software#.NET) - a piece of software that wrapps your Database in C# classes so you can "use" your DB by using the created classes - if you need to go from "typename" to "Type" you probably need to go via "System.Reflection" like so : https://stackoverflow.com/questions/179102/getting-a-system-type-from-types-partial-name ... but to be needing to do that smells. Badly. You probably have some kind DropDown that is populated with your database tables names - so "somewhere" you already have a list of types you want colums of. – Patrick Artner Dec 09 '17 at 22:46

2 Answers2

3

typeof(Company).GetProperties() isn't fetching the names of the columns of your table at all though - it's fetching the names of the properties of the C# class/entity that you're using to represent that table. Do you already have a process to guarantee you have C# entities with property names that exactly match the column names of your tables? If so, you can do the same principle (but instead of using typeof(Company) you'd need to find the type by name, which there are various ways of doing). If not, you'll need to to actually query the database to find the column names.

Dylan Nicholson
  • 1,301
  • 9
  • 23
0

As far as i know, you can use the Type parameter.

public List<string> GetColumnNames(Type tablename)
{

    var names = tablename.GetProperties().Select(p => p.Name).ToArray();

    return names.ToList();
}
Michael
  • 120
  • 9
  • `tablename` is misleading - and unless the table he want's is already provided as Class by whatever orm mapper he is using to fetch his objects from the database this won't do much good. – Patrick Artner Dec 09 '17 at 22:33