开发者

LINQ: Get Table details

I'm using LINQPad and I would like to know schema details of a table.

I know that I do it using SQ开发者_JS百科L:

SELECT column_name,* 
FROM information_schema.columns
WHERE table_name = '{table_name}'
ORDER BY ordinal_position

How can I do this using LINQ?


LINQ to SQL contexts have a Mapping property that you can use for this sort of thing. A query like the one you provided might look something like this:

from t in context.Mapping.GetTables()
where t.TableName == "[table_name]"
from c in t.RowType.DataMembers
orderby c.Ordinal
select new {columnName = c.Name, columnInfo = c}

See this answer for more details.


MetaTable t = MyDataContext.Mapping.GetTables().Where(
   i => i.TableName == "TABLENAME").SingleOrDefault();
PropertyInfo[] fields = t.RowType.InheritanceRoot.GetType().GetProperties();

'fields' will contain the names and types of the columns.


In LINQ to SQL, you could try to bring in these views into the model. If that does not work, you could create a stored procedure that retrieves the info, then LINQ to SQL maps the stored procedure and you execute it as a function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜