How can one get a column name from a Linq-To-Entites object

All --

Please help.

How can one get a column name from a Linq-To-Entites object?

For example, see this code...

 

//With ADO, I can get a column name something like this...

DataTable myTable = new DataTable();
myTable.Columns.Add(new DataColumn("myColName", typeof(string));
string myValue = myTable.Columns[0].ColumnName;


//With Linq, I want to get (but cannot get) a column name something like this...

 



 

public static TestProject.Data.Entities.TestEntity GetEntity(Guid targetPkID)
{
 Data.Components.Ssbi myEntity = null;

 using (Data.Entities.TestDataContext myContext = new Data.Entities.TestDataContext())
 {
  var q = from p in myContext.TestEntity
    where (p.PkID == targetPkID)
    select p;

  if ((q == null) || (q.Count() <= 0) || (q.First() == null))
  {
   myEntity = null;
  }
  else
  {
   myEntity = (Data.Entities.TestEntity)(q.First());

   //This does not compile...
   string myColumnName = myEntity.CustomerSite.ColumnName;
  }
 }

 return myEntity;
}

 

Please advise.

Thank you.

-- Mark Kamoski


http://www.NetBrainer.com
0
mkamoski
1/16/2009 3:52:48 PM
asp.net.sql-datasource 29906 articles. 0 followers. Follow

2 Replies
772 Views

Similar Articles

[PageSpeed] 0

Not really sure how your namespacing is setup but this code might get you pointed in the right direction with a little tweeking.

using (Data.Entities en = new Data.Entities())
{
	ObjectQuery qry = new en.TestEntity();
	var entity = qry.Context.MetadataWorkspace.GetItem("Data.TestEntity", true, DataSpace.CSpace);
	var member = entity.Members[0];

	string m = member.Name;
}
You are basically querying the Metadata using MetadataWorkspace.

- William

Please mark the most helpful reply/replies as "Answer".

Give some of my PWSK modules a try.
0
whighfield
1/18/2009 4:51:42 AM

whighfield:

....

 var entity = qry.Context.MetadataWorkspace.GetItem("Data.TestEntity", true, DataSpace.CSpace);

...

That works but it is hardcode.

That is just a step above building raw SQL strings, no?

It is nice to know, however, and it is a good first step-- but, I need the other part to have a complete answer.

How can one get rid of that hardcode?

BTW, the same problem exists with ObjectQuery.Include(string) -- just a bad way to do it by passing hardcoded names, IMHO.

What do you think?

Please advise.

Thank you.

-- Mark Kamoski


http://www.NetBrainer.com
0
mkamoski
1/22/2009 7:37:38 PM
Reply: