locked
Get column names RRS feed

  • Question

  • I've created a LINQ to SQL file for a database that only contains one table.  I need a way to grab the column names from the table.  Is there any way to achieve this with LINQ?

     

    Thanks for any help.

     

    Wednesday, November 26, 2008 5:29 PM

Answers

  • You can get this information by asking the mapping used by your data context for its meta data. You need to pass the type of the entity in your table:

     

            using (DbDataContext db = new DbDataContext())
            {
                var metaType = db.Mapping.GetMetaType(typeof(MyTestEntity));

                foreach (var member in metaType.DataMembers)
                {

                    // "Property/field name" - "Column name" - "Column?"
                    Console.WriteLine("{0} - {1} - {2}", member.Name, member.MappedName, member.IsPersistent);
                }
            }

     

     

     

    Thursday, November 27, 2008 9:41 AM
    Answerer

All replies

  • You can get this information by asking the mapping used by your data context for its meta data. You need to pass the type of the entity in your table:

     

            using (DbDataContext db = new DbDataContext())
            {
                var metaType = db.Mapping.GetMetaType(typeof(MyTestEntity));

                foreach (var member in metaType.DataMembers)
                {

                    // "Property/field name" - "Column name" - "Column?"
                    Console.WriteLine("{0} - {1} - {2}", member.Name, member.MappedName, member.IsPersistent);
                }
            }

     

     

     

    Thursday, November 27, 2008 9:41 AM
    Answerer
  • Thanks to both of you.  I was able to get the information that I needed.

    Monday, December 1, 2008 4:42 PM