Solved

Get Primary Key of table using only string type table name in Linq

Posted on 2009-05-13
3
633 Views
Last Modified: 2013-11-11
Hello,
I'm having trouble with obtaining the primary key using the LINQ datacontext and when I only have the table name in a string variable.  What I am trying is below, but it doesn't work because I can't get the Type argument of GetTable

I'm building a dynamic table editor using the GridView control and LINQDataSource as the datasource so I need to get the primary key of the table being edited so I can assign it to the GridView.DataKeyNames property.

Anyone run into this?
//This works because I write the actual entity name 'MainTable' in the code:

 

string pkName = _dc.Mapping.GetTable(typeof(MainTable)).RowType.IdentityMembers[0].Name;

 

//But I want to dynamically get the primary key of a table that is stored in a string variable.

//I have tried this:

 

Type tableType = Type.GetType("My.Namespace.." + _tableName);

pkName = _dc.Mapping.GetTable(typeof(tableType)).RowType.IdentityMembers[0].Name;

 

//This doesn't work because it wants an actual class name in the GetTable() method.

Open in new window

0
Comment
Question by:Ken Fayal
3 Comments
 
LVL 13

Expert Comment

by:dungla
ID: 24381034
Hi,
You can use Reflection to do that.
//first getting assembly
Assembly assem = Assembly.LoadFrom("fullname");
Type t = assem.GetType("typename");
0
 
LVL 7

Accepted Solution

by:
deadlyDev earned 500 total points
ID: 24448786
Hey Kenbo...

From your query I think you are looking for something like this?
var pkName = dc.Mapping.GetTables()

                .Where(c => c.TableName == _tableName)

                .Single()

                .RowType.IdentityMembers[0].Name;

Open in new window

0
 
LVL 9

Author Closing Comment

by:Ken Fayal
ID: 31581264
Yes!!! This was it!  Thank you!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now