5,660,782 members and growing! (15,857 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » LINQ » General     Advanced License: The Code Project Open License (CPOL)

LINQ-to-SQL: Generic Primary Key function

By Bob Housedorf

This a vb.net version of code in a blog entry by Chris Sainty
VB (VB 9.0, VB), .NET (.NET, .NET 3.0), Dev

Posted: 26 Jun 2008
Updated: 26 Jun 2008
Views: 4,047
Bookmarked: 4 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.07 Rating: 2.25 out of 5
1 vote, 33.3%
1
1 vote, 33.3%
2
0 votes, 0.0%
3
1 vote, 33.3%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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

http://csainty.blogspot.com/2008/04/linq-to-sql-generic-primary-key.html

Since it took a little while to convert 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 one business entity for whatever purpose needed.

Create your new module called DataContextHelpers, as in the following.

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

     _
    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", _
                                mapping.TableName))
        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 

and

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

Note that even with the import, VS 2008 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. system.data.linq being the big one to get this to compile.

License

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

About the Author

Bob Housedorf


Supporter

Occupation: Software Developer (Senior)
Location: United States United States

Other popular LINQ articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jun 2008
Editor: Chris Maunder
Copyright 2008 by Bob Housedorf
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project