[dropcap]Hi[/dropcap] everybody,
In this tutorial, i would like to share with you a cool stuff called Linq to SharePoint. It a new way to write queries. If you already bored by Caml syntax, i definitely suggest you to use Linq to SharePoint . Before we start lets take a look at some quick info.

What is Linq?
Language Integrated Query (LINQ, pronounced “link”) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages
LINQ is a new data access paradigm which allows users to express SQL like syntax against a variety of data sources. LINQ can improve performance by allowing the back end data source to decide the best way to solve the query.

What is LINQ to SharePoint?
It is a entegration for sharepoint lists to make queriny simple query and alternative of Caml systax. SharePoint now fully supports LINQ for querying lists so that you can query information from the platform in a more condensed, easier to understand format.

What is SPMetal.exe?

SPMetal is a command-line tool that generates entity classes, which provide an object-oriented interface to the Microsoft SharePoint Foundation content databases. These classes are primarily used in LINQ to SharePoint queries; but they are also used to add, delete, and change list items with concurrency conflict resolution. Finally, they can be used as an alternative to the regular SharePoint Foundation object model for referencing content.

SPMetal is typically executed as a prebuild command in Microsoft Visual Studio because the code it generates is usually part of a solution that includes a new site type that is itself evolving during the development cycle. Because SPMetal overwrites the files it generates with each execution, for a given output path and against a given Web site, you should not make changes to these files. The classes it generates are partial, so you can add members to them in separate, manually created, code files.

 

In order to work with LINQ in SharePoint 2010, we have to use a tool called SPMetal.exe You can find this file under “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin” . We are going to use this file to create some entity classes which Visual Studio 2010 can use to get IntelliSense. It will also allow for LINQ-based queries to be run on your lists. To do this you should use following statement to create entity class file.


spmetal.exe /web:http://SharepointTest/MySite/ /namespace:MyLinq /code:Mylinq.cs

If your solution is based on an existing site type, such as the Team Site that is included with SharePoint Foundation, and it references only lists that are always present in such sites and only fields (columns) that are always present in those lists, you can run SPMetal just once and make changes to the generated file that will not be overwritten.I used a batch file to create entity-classes CreateLinq.bat. Because whenever I create new list or add new column, I must update entity class to use lists and columns. Instead writing every time I needed to update my entitiy class I use only double click.


spmetal.exe /web:http://SharepointTest/MySite/ /namespace:MyLinq /code:Mylinq.cs /code:MyLinq.cs
pause

We created entity class file now it is time to add Microsoft.Sharepoint.Linq reference.

Make sure that you use using statement for system.linq and sharepoint.linq


using System.Linq;
using Microsoft.SharePoint.Linq;
using MyLinq;

Lets get down to coding

Create a item



MylinqDataContext context = new MylinqDataContext("http://SharepointTest/MySite");
EntityList<SampleDataItem> sampledatalist = context.GetList<SampleDataItem>("SampleData");
SampleDataItem itemToInsert = new SampleDataItem()
   {
     Title= "Demo Title"
   };
sampledatalist.InsertOnSubmit(itemToInsert);
context.SubmitChanges();

To create an item first create context object using properties of MyLinq file we have created above. Second, get items of SampleData list assign to “sampledatalist”. We create a object named itemToInsert which is SampleDataItem object and set field of item that we will create. Then we push the value of itemToInsert object to sampledata and finally submit changes to context.

Read item from List



MylinqDataContext context = new MylinqDataContext ("http://SharepointTest/MySite");

var sampQuery = from samp in SampleData select samp;

GridView1.DataSource = sampQuery;
GridView1.DataBind();

Using context we query for data. Use this data as a datasource for gridview. Finally bind datasource to gridview and there you go. If you set autogeneratecolumns=”true” you will see every every column in your list.

Update one List item



MylinqDataContext context = new MylinqDataContext ("http://SharepointTest/MySite");

var itemToUpdate = (from r in context.SampleData where r.Rep == TextBox2.Text select r).First();
itemToUpdate.Title = "Update with this text";
context.SubmitChanges();

Again using context which is our entity class query for the certain item. We do this by First() . Then set the value of Title and finally submit changes to context.

Update multiple List items



MlinqDataContext context = new MlinqDataContext("http://SharepointTest/MySite");

var itemToUpdate = from r in context.SampleData where r.Rep == TextBox5.Text select r;
foreach(var t in itemToUpdate)
   {
     t.Title = TextBox6.Text;
    }
context.SubmitChanges();

I think you already understand the idea of DataContext. First we query for list item that we will update. Using foreach statement we are you to manipulate every single one of them. Here i am using TextBox6 value to set Title field. Lastly submit changes to context.

Delete List item



MylinqDataContext context = new MylinqDataContext ("http://SharepointTest/MySite");

EntityList<SampleDataItem> sampledata= context.GetList<SampleDataItem>("SampleData");
var itemToDelete = (from r in context.SampleData
                           where r.Id == Convert.ToInt32(TextBox4.Text) select r).First();
sampledata.DeleteOnSubmit(itemToDelete);
context.SubmitChanges();

Here there is one more row different than other is that we have sampladata entity list. We used because fist query for list item to delete, then we say delete these item from sampledata list. Then using deleteonsubmit set the items that will be deleted in sampledata list and submit changes to context.