score:1
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> ();
Source: stackoverflow.com
Related Query
- How would I get the column names from a Model LINQ?
- How would I use LINQ to XML to get the value from example XML
- how to get the row value of each column from linq
- How to get Column Names using Linq from DataTable
- How I get the right Data from my Column with Linq
- How do I use LINQ to select from DataTable, with a list of the required column names
- How to get the TSQL Query from LINQ DataContext.SubmitChanges()
- How to get the latest date inserted from the table with Linq to entities
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to get Month Name from datetime column in Linq to SQL?
- How does linq actually execute the code to retrieve data from the data source?
- How to get only Date from datetime column using linq
- How to use expressions to build a LINQ query dynamically when using an interface to get the column name?
- How do I get results from a Linq query in the order of IDs that I provide?
- How to get the last value from a model list without looping it in MVC?
- How to get column names from table returned from stored procedure
- C# LINQ How to get a data source from a db?
- How can I get the top three players and their high scores from a collection using linq and lambdas
- How do I get the first value from this collection using Linq to Entities?
- How to get only two fields from the table by linq query?
- How to get Database names from given Sql Server in LINQ
- How to get the second repeated item from a collection of objects using LINQ to object
- How to get the distinct record name and its each record count to bind to gridview in C# using Linq from SQL Server
- How can I use Linq with JSON to get all the Ids from this odd JSON?
- How to get the generated Lucene query from a LINQ query
- How to use LINQ to get the first matching file from inside a ZipArchive?
- How do I return the column names of a LINQ entity
- How to get only the max string length column row from a table using linq?
- How to Get The Values Printed from XML using LINQ or XPath
- How to get the Inner Element value from Elements in xml linq
More Query from same tag
- How does EF create the SQL needed to update one million rows in a SQLite table with five million rows? Does it do one by one row updates?
- EntityFramework: Add Where clause to ObjectSet Query to Select on CHILD attributes
- I have two large lists and I need get the diff between them
- LINQ to Entities does not recognize the method
- Can this be done only with c# linq
- Should a reference to a null variable throw exception
- Using Linq(?) to get a property from a list inside of a list
- How to check record exists in group by subquery in linq to sql
- Linq: Model does not bind properly to Database View(query with UNION)
- Use Count in Linq returns different results than SQL query
- SQL Query union two tables, search multiple columns by multiple keywords
- Linq where clause using filter object
- Help with a LINQ Query
- Generic c# Round Robin (partitioned/sorted) Queue
- Get a random item from a collection that does not already exist in another collection - LINQ?
- how to use Linq to generate unique random number
- Custom Linq extension method for jqGrid select list
- only 10 result items returning to the page out of 250 items
- Find exact substring in string array using LINQ in C#
- LINQ: How to dynamically use an ORDER BY in linq but only if a variable is not string.empty or null
- Linq XML query - How do I return a node that meets a condition within its own nested nodes?
- How can I stop intellisense from auto completing 'from' when typing linq query
- Convert one Collection to another Collection (not List)
- Execute stored procedure in ASP.NET MVC and LINQ
- linq filtering child collection
- Any caveats with casting T[] to IList<T>?
- Extracting specific data from an xml file using xdocument in C#
- Is looping through the entityreference the correct way?
- Linq query using result of another query
- How to group by custom types with linq