问题描述
我正在使用 LINQ to SQL 查询在我的应用程序中返回数据.但是我发现我现在需要返回列名称.尽我所能尝试,我完全无法在互联网上找到如何做到这一点.
因此,如果我的 LINQ 实体表具有属性(姓氏、名字、中间名),我需要返回:
Last_name First_Name Middle_name
而不是通常的
Smith John Joe
推荐答案
您当然可以使用一些 LINQ-To-Xml 直接针对".edmx"文件或已编译程序集中的嵌入式模型资源来实现.
以下查询获取字段(而非列)名称.如果您需要这些列,则只需更改查询以适合.
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), };
问题描述
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.
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
rather than the usual
Smith John Joe
推荐答案
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), };