Thursday, September 25, 2008

LINQ To SQL

My first Attempts to use LINQ were breifly held back until I realised the beta version had slightly different functions availiable, the diffrences are outlined here. Add has been replaced by InsertOnSubmit and Remove by DeleteOnSubmit, becareful when looking at the example on the web, most were written using the beta release...

After this I found it was very easy to create a fully functional set of methods for Insert, Update and Get, (no Delete as it not required by the system).

GET
DataContext objDB = new DataContext();

var temp = from hi in objDB.Help
where hi.Identifier == ID
select hi;


UPDATE
DataContext objDB = new DataContext();
Help temp = from hi in objDB.Help
where hi.Identifier == ID
select hi;

temp.Author = "Harry";
objDB.SubmitChanges();

INSERT
Help temp = new Help();
// Populate object to insert
DataContext objDB = new DataContext();
objDB.Help.InsertOnSubmit(temp);
objDB.SubmitChanges();

Obviously there is a little more to the work than this, but I won't repeat too much of what is already out there, for example: Scott Guthrie's Blog and The Linq Project.

0 Comments:

home | www.purplepool.com