Click here to Skip to main content
13,300,654 members (53,758 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 26 Jun 2008

LINQ-to-SQL: A Generic Primary Key Function

, 26 Jun 2008
Rate this:
Please Sign up or sign in to vote.
This a VB.NET version of a code in a blog entry by Chris Sainty.


Looking around the web today for a method to generically update a property from a DataGrid, using LINQ, I finally found what I was looking for, at:

Since it took a little while to convert it to VB.NET, I figured I would post it here for others to use.

Using the code

Since I'm not yet a very experienced writer, I'll leave it for the reader to make sense of where and how best to use this code. In a nutshell, this code will work in concert with LINQ to SQL to allow you to retrieve a business entity for whatever purpose needed.

Create your new module called DataContextHelpers, as follows:

Imports System.Runtime.CompilerServices

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.Linq
Imports System.Linq.Expressions

Public Module DataContextHelpers

    <extension()> _
    Public Function GetByPk(Of T As Class)(ByVal context As DataContext, _
                                           ByVal pk As Object) As T
        Dim table = context.GetTable(Of T)()
        Dim mapping = context.Mapping.GetTable(GetType(T))
        Dim pkfield = mapping.RowType.DataMembers.SingleOrDefault(Function(d) d.IsPrimaryKey)

        If (pkfield Is Nothing) Then
            Throw New Exception(String.Format("Table {0} does not " & _ 
                      "contain a Primary Key field", _
        End If

        Dim param = Expression.Parameter(GetType(T), "e")
        Dim predicate = Expression.Lambda(Of Func(Of T, Boolean)) _
                        (Expression.Equal(Expression.Property(param, pkfield.Name), _
                                          Expression.Constant(pk)), _
                                          New ParameterExpression() {param})
        Return table.SingleOrDefault(predicate)
    End Function

End Module

To use this in your code, simply:

Imports yournamespace.DataContextHelpers 


Dim thisBusinessEntity As businessEntity = db.GetByPk(Of businessEntity )(key) 

Note that even with the import, VS2008 still didn't give me full intellisense when I was working on "Common" intellisense mode. It did show up in "All" mode. Also, make sure your project is set to use .NET 3.5, and you have added all the usual references, being the big one to get this to compile.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bob Housedorf
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generaltrying to use it! Pin
Yisman218-Nov-10 7:24
memberYisman218-Nov-10 7:24 
GeneralC# Version [modified] Pin
Member 384770620-Dec-08 16:25
memberMember 384770620-Dec-08 16:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 26 Jun 2008
Article Copyright 2008 by Bob Housedorf
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid