Feb 14, 2010

Programming LINQ in VStudio 2010

  • LINQ to sql class
Programming LINQ to SQL in VStuido 2010 is as simple as step 1, 2, 3:
Step 1: launch VStudio 2010 to create an empty SharePoint Project:

Step 2: add a new item and choose LINQ to SQL Classes. Name it AWEmployee.dbml

Step 3: launch Server Explorer to drag Employee tables into design surface. In AWEmpolyee.designer.cs, you will find a class named as AWEmployeeDataContext.

  • LINQ to SharePoint
Just as SharePoint lists can now be displayed in VStudio 2010's Server Explorer, LINQ to SharePoint is another indication that MS start to treat SharePoint as a datasource. LINQ to SharePoint is not as mature as LINQ to SQL nevertheless. Currently it needs a manual process(as compared to the VS build-in entity framework for sql) , but fairly simple, to implement it:

Step 1 use SPMetal utility to generate a datacontext or entity class, for eaxmple: SPMETAL /web:http://sp2010/ /namespace:sp2010 /code: EntityClasses.cs

Step 2: the resulting source file defines a datacontext class which in turn defines an inner class for each individual SharePoint list (update 04/19/2010: or using parameters tag to define what you need to generate: /paramters:parameters.xml, see here for details) Add this file into VStudio project;

Step 3: Start query SharePoint List data by using LINQ query such as:
var query= from c in EntityClassesDataContext.Contacts where !c.ListName.Equal(""); orderby c.FirstName select c;
The advantage of using LINQ is, they are all strongly typed, and you get all intellisense.