[英]How do I return the column names of a LINQ entity
I am using LINQ to SQL queries to return data in my application. 我正在使用LINQ to SQL查询来返回我的应用程序中的数据。 However I find it is now needful for me to return the column Names.
但是我发现现在需要返回列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: 因此,如果我的LINQ实体表具有属性(Last_Name,First_name,Middle_Name),我需要返回:
Last_name
First_Name
Middle_name
rather than the usual 而不是通常的
Smith
John
Joe
Lets assume you're talking about the Contact
Table in the assembly named YourAssembly
in a Context called MyDataContext
让我们假设你在一个名为
YourAssembly
的Context中讨论名为MyDataContext
的程序YourAssembly
的Contact
Table
Using Reflection against a Table 对表使用反射
You can use reflection to get the properties like you would any type 您可以使用反射来获取类似于任何类型的属性
var properties = from property in Type.GetType("YourAssembly.Contact").GetProperties() select property.Name ;
var属性=来自Type.GetType中的属性(“YourAssembly.Contact”)。GetProperties()select property.Name;
foreach (var property in properties) Console.WriteLine(property);
As shaunmartin notes this will return all properties not just Column Mapped ones. 正如shaunmartin所说,这将返回所有属性,而不仅仅是列映射的属性。 It should also be noted that this will return Public properties only.
还应注意,这将仅返回公共属性。 You'd need to include a BindingFlags value for the bindingAttr Parameter of GetProperties to get non-public properties
您需要为GetProperties的bindingAttr参数包含BindingFlags值以获取非公共属性
Using the Meta Model 使用元模型
You can use the Meta Model System.Data.Linq.Mapping to get the fields ( I added IsPersistant to only get the Column Mapped properties) 您可以使用Meta Model System.Data.Linq.Mapping来获取字段(我添加了IsPersistant以仅获取Column Mapped属性)
AttributeMappingSource mappping = new System.Data.Linq.Mapping.AttributeMappingSource();
var model = mappping.GetModel(typeof (MyDataContext));
var table = model.GetTable(typeof (Contact));
var qFields= from fields in table.RowType.DataMembers
where fields.IsPersistent == true
select fields;
foreach (var field in qFields)
Console.WriteLine(field.Name);
Using Reflection from a query result 使用查询结果中的反射
If on the other hand you wanted it from a query result you can still use reflection. 另一方面,如果您想从查询结果中获取它,您仍然可以使用反射。
MyDataContextdc = new MyDataContext();
Table<Contact> contacts = dc.GetTable<Contact>();
var q = from c in contacts
select new
{
c.FirstName,
c.LastName
};
var columns = q.First();
var properties = (from property in columns.GetType().GetProperties()
select property.Name).ToList();
You could certainly do it with some LINQ-To-Xml directly against the ".edmx" file or the embedded model resources in the compiled assembly. 您当然可以使用一些LINQ-To-Xml直接对“.edmx”文件或编译的程序集中的嵌入式模型资源执行此操作。
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),
};
I stumbled upon this answer to solve my own problem and used Conrad Frix 's answer. 我偶然发现了这个答案来解决我自己的问题,并使用了康拉德弗里克斯的答案。 The question specified VB.NET though and that is what I program in. Here are Conrad's answers in VB.NET (they may not be a perfect translation, but they work):
这个问题指的是VB.NET,这就是我编程的内容。这里是Conrad在VB.NET中的答案(它们可能不是一个完美的翻译,但它们有效):
Example 1 例1
Dim PropertyNames1 = From Prprt In Type.GetType("LocalDB.tlbMeter").GetProperties()
Select Prprt.Name
Example 2 例2
Dim LocalDB2 As New LocalDBDataContext
Dim bsmappping As New System.Data.Linq.Mapping.AttributeMappingSource()
Dim bsmodel = bsmappping.GetModel(LocalDB2.GetType())
Dim bstable = bsmodel.GetTable(LocalDB.tblMeters.GetType())
Dim PropertyNames2 As IQueryable(Of String) = From fields In bstable.RowType.DataMembers
Where fields.IsPersistent = True
Select fields.Member.Name 'IsPersistant to only get the Column Mapped properties
Example 3 例3
Dim LocalDB3 As New LocalDBDataContext
Dim qMeters = From mtr In LocalDB3.tblMeters
Select mtr
Dim FirstResult As tblMeter = qMeters.First()
Dim PropertyNames3 As List(Of String) = From FN In FirstResult.GetType().GetProperties()
Select FN.Name.ToList()
To display the results: 要显示结果:
For Each FieldName In PropertyNames1
Console.WriteLine(FieldName)
Next
For Each FieldName In PropertyNames2
Console.WriteLine(FieldName)
Next
For Each FieldName In PropertyNames3
Console.WriteLine(FieldName)
Next
Please also read Conrad's answer for notes on each method! 另请阅读康拉德关于每种方法的说明的答案!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.