如何返回LINQ实体的列名 [英] How do I return the column names of a LINQ entity
问题描述
我正在使用LINQ to SQL查询来返回我的应用程序中的数据.但是,我发现现在需要返回名称"列.请尝试,因为我可能完全无法在互联网上找到该方法.
I am using LINQ to SQL queries to return data in my application. However I find it is now needful for me to return the column Names. Try as I might I have been completely unable to find out how to do this on the internet.
因此,如果我的LINQ实体表具有属性(姓,名,中间名),我需要返回:
So if my LINQ entity table has the properties (Last_Name, First_name, Middle_Name) I need to return:
Last_name
First_Name
Middle_name
而不是通常的
Smith
John
Joe
推荐答案
您当然可以使用某些LINQ-To-Xml直接针对".edmx"文件或已编译程序集中的嵌入式模型资源执行此操作.
You could certainly do it with some LINQ-To-Xml directly against the ".edmx" file or the embedded model resources in the compiled assembly.
以下查询获取字段(而非列)名称.如果需要这些列,则只需更改查询即可.
The below query gets the field (not column) names. If you need the columns then just change the query to suit.
var edmxNS = XNamespace.Get(@"http://schemas.microsoft.com/ado/2007/06/edmx");
var schemaNS = XNamespace.Get(@"http://schemas.microsoft.com/ado/2006/04/edm");
var xd = XDocument.Load(@"{path}\Model.edmx");
var fields =
from e in xd
.Elements(edmxNS + "Edmx")
.Elements(edmxNS + "Runtime")
.Elements(edmxNS + "ConceptualModels")
.Elements(schemaNS + "Schema")
.Elements(schemaNS + "EntityType")
from p in e
.Elements(schemaNS + "Property")
select new
{
Entity = e.Attribute("Name").Value,
Member = p.Attribute("Name").Value,
Type = p.Attribute("Type").Value,
Nullable = bool.Parse(p.Attribute("Nullable").Value),
};
这篇关于如何返回LINQ实体的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!