LINQ:获取表的详细信息详细信息、LINQ

2023-09-04 08:29:05 作者:過去、過不去

我用LINQPad,我想知道表的架构细节。

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

我知道我做的使用SQL的:

I know that I do it using SQL:

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

我怎样才能做到这一点使用LINQ?

How can I do this using LINQ?

推荐答案

LINQ to SQL的上下文有一个映射属性,您可以使用这样的事情。就像你所提供可能看起来像这样一个查询:

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}

请参阅this回答更多的细节。