score:1

Accepted answer

i am sorry, i don't have working experience with linq.

this is purely based on looking at msdn.

datacontext has a mapping property, which returns an instance of metamodel.
metamodel has getmetatype, which takes a type. in your case it could be typeof(project).
getmetatype returns a metatype which has the getdatamember method, which takes a memberinfo parameter. you will have to use reflection on your projects object to get the memberinfo object.
the metadatamember instance returned by getdatamember should have all the things, you need.

i hope i am somewhat in right direction (purely looking at msdn & traversing)

score:0

your columns should be mapped as properties on your project model. i'm not sure if you can get the underlying database structure when using linq to sql. the entire point of linq to sql is to abstract the database away.

score:0

here an another way:

        public string[] getcolumnnames()
        {
            var propnames = getpropertynames(_context.users);
            return propnames.toarray();
        }

        static ienumerable<string> getpropertynames<t>(ienumerable<t> lst)
        {
            foreach (var pi in typeof(t).getproperties())
            {
                yield return pi.name;
            }
        }

score:2

using todd smiths(+1) solution you get all properties (included entity sets, etc). to filter out all non-column properties this will do the trick:

var columnnames = db.columnnames<orders>().where(n => n.member.getcustomattributes(typeof(system.data.linq.mapping.columnattribute), false).firstordefault() != null).select(n => n.name);

score:3

your projects wrapper will have a set of properties each with a [column] attribute. so just use reflection to enumerate the properties with that attribute.

score:6

thanks guys, you got me started on the right track. i found my solution with the following code. i can then iterate through the datamembers and pull out their individual properties such as name, type, etc.

var db = new gmpdatacontext();
var columnnames = db.mapping.mappingsource
                    .getmodel(typeof(gmpdatacontext))
                    .getmetatype(typeof(project))
                    .datamembers;

score:15

this would be nice to have as an extension method:

public static class linqextensions
{
  public static readonlycollection<metadatamember> columnnames<tentity> (this datacontext source)
  {
      return source.mapping.mappingsource.getmodel (typeof (datacontext)).getmetatype (typeof (tentity)).datamembers;
  }
}

example:

var columnnames = mydatacontext.columnnames<orders> ();

Related Query

More Query from same tag