Add/Update/Delete with LINQPad

December 5, 2007 2:42 AM

Danny Douglass recently posted about LINQPad, a query tool that understands LINQ. I spent some time today using it and am very impressed. It did take a while, however, to look through the numerous sample scripts and figure out exactly how to insert/update/delete data, so I thought I would share what I've found. The biggest changes between working in LINQ-To-SQL in a Visual Studio 2008 project and writing a LINQ query in LINQPad are the lack of a DataContext, the difference between C# Expressions and C# Statements, and the addition of a Dump command.

No Data Context: When you need to interact with a database via LINQ in a Visual Studio 2008 project, you do so by creating a LINQ-To-SQL DBML file that generates a data context for you. This data context is in charge of maintaining your database connection and is what you use to submit changes to the database. Because there is no data context readily available to you (and no .dbml file) in LINQPad, the way you go about this is slightly different. There is a globally-scoped subroutine, "SubmitChanges()," that should be called whenever you wish to commit an action to the database.

C# Expressions vs. C# Statements: By default LINQPad opens in "C# Expression" mode. In this mode you can type a simple query like "from r in Regions select r" and run it to see the results. As far as I can tell there is no way to insert/update/delete data in this mode. By switching to "C# Statement(s)" mode you are able to declare variables, control flow statements, and reference objects; this is the mode you need to be in to insert/update/delete data. To enter "C# Statement(s)" mode, select it from the "Type" drop-down box at the top of the window as show below:

LINQPad

object.Dump(): When you write a query in "C# Expression" mode the result of that query is automatically rendered to the Results frame (shown below). Because "C# Statement(s)" mode gives you the capability to run many queries in one execute, displaying the results of those queries must be manually invoked. To do so simply call the ".Dump()" method on the resultset of the query you want to display. ".Dump()" is implemented as an extension method available on all objects, so whether you are retrieving a single object, a list of objects, or an anonymous type the Dump method will be available to display your data.

LINQPad-Results

Below I have included examples of several ways to query data, as well as an example each for inserting, updating, and deleting data.

    1 // select (LINQ Syntax)

    2 var regions =

    3     from r in Regions

    4     where r.RegionID > 0

    5     select r;

    6 regions.Dump();

    7 

    8 // insert

    9 Region newRegion = new Region()

   10 {

   11     RegionID = 99,

   12     RegionDescription = "Lorem ipsum..."

   13 };

   14 Regions.InsertOnSubmit( newRegion );

   15 SubmitChanges();

   16 

   17 // select (LINQ Syntax, no temp variable)

   18 (from r in Regions

   19     where r.RegionID > 0

   20     select r).Dump();

   21 

   22 // update

   23 Region region =

   24     (from r in Regions

   25         where r.RegionID == 99

   26         select r).Single();

   27 region.RegionDescription = "...dolor sit amet...";

   28 SubmitChanges();

   29 

   30 // select (.Where Lambda expression)

   31 ( Regions.Where( r => r.RegionID > 0 ) ).Dump();

   32 

   33 // delete

   34 Region removeRegion =

   35     Regions.Where( r => r.RegionID == 99 ).Single();

   36 Regions.DeleteOnSubmit( removeRegion );

   37 SubmitChanges();

   38 

   39 // select (Regions & Territories, joined and

   40 //        combined by anonymous type)

   41 (from r in Regions

   42     join t in Territories

   43         on r.RegionID equals t.RegionID

   44     select new{

   45         Region = r,

   46         Territory = t

   47     }).Dump();

Tags: ,
Categories: LINQ
Actions: E-mail | Permalink | Comments (5) RSS Feed for this post's comments.

Comments

12/5/2007 2:47 AM #

trackback


Trackback from: DotNetKicks.com

1/17/2008 7:39 AM #

Mark Brindle

Great post thanks.

I have a table called 'TransactionProcessSettings'

I was expecting a class called 'TransactionProcessSetting' but there is none.
I can't seem to Add rows.

How do I know what classes have been created?

I have a table called 'Users' and it created a 'User' class.

The lack of a Data Context is frustrating.

Any ideas?

Mark Brindle au

1/17/2008 10:22 AM #

Troy Goode

Hi Mark,

Take a look at your database explorer (the top-left panel in LINQPad). If you've used that to add a database connection (which you'll have to do to execute the query) you can expand the database and it will list all of the tables using their LINQPad-ified names.

The convention in LINQPad is to pluralize table names, so I would suspect that if your table is named "TransactionProcessSettings" in the database, it will also be called that in LINQPad.

Let me know if you have any further problems!

Troy

Troy Goode us

1/17/2008 7:55 PM #

Mark Brindle

Hi Troy. I did not explain myself very well last night - Very late here in OZ when I did that post.

If you have a table named 'User' you get two classes - User and Users.

If your table is named 'Users' you only have one class names Users.

That is my experience so far.

Cheers

Mark Brindle au

2/12/2008 2:36 PM #

Troy Goode

Sorry for the wickedly late reply Mark, but I went to try and verify what you are saying and can not reproduce the issue.

I created a brand new database (database1) and added one table: "User" with one column "UserName" (nvarchar-50). I then went into LINQPad and clicked "Add connection" and selected the db file while making sure that both "Pluralize EntitySet and Table properties" and "Capitalize property names" are checked.

I am able to run this query "from u in Users select u" successfully in "C# Expression" mode. If you are still having trouble with this can you give me the steps to reproduce with a fresh db?

Troy Goode us

Add Comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading




Troy Goode

Troy Goode
Microsoft Certified Professional Developer
AddThis Feed Button

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008

Colophon

Powered by:
BlogEngine.NET 1.4
Template:
Designs by Darren
Header Font:
Stamper
Syntax Highlighting:
WLW Code Snippet Plugin