As you know, LINQ to SharePoint List works the same way as LINQ to Object, LINQ to DataSet, and LINQ to SQL. LINQ to SharePoint Provider is integrated with SharePoint 2010, and is defined in Microsoft.SharePoint.Linq to help us work with SharePoint List faster, more easily, and without any CAML Query commitment.
How can I use LINQ to query data from SharePoint Lists in 2010?
There are two solutions available which will allow you to use LINQ to query a List in SharePoint 2010.
I. Using LINQ to Objects to query a SharePoint List
This is the way that most of developers tend to think of since LINQ to SharePoint Provider was not supported in the earliest beta version of SharePoint 2010.
The code sample below shows you how to use LINQ to work with SPListItem:
or, if you prefer lambda expressions:
In this example, it queries products which contain the string computer in its title.
II. Using LINQ to SharePoint Provider to query SharePoint List
Just as LINQ to SQL Provider provides System.Data.Linq.DataContext, which uses the GetTable() method that returns a Table<TEntity> object, LINQ to SharePoint Provider provides the Microsoft.SharePoint.Linq.DataContext class which uses a GetList<T> method that returns an EntityList<TEntity> class. It simply translates LINQ queries into Collaborative Application Markup Language (CAML) queries. As a result, it’s no longer necessary for you to write CAML queries.
Here is an example that queries customers who are living in New York.
Another example, to add an item to the List:
To delete data, you can query it and then use DeleteOnSubmit() to delete it. To update data, it’s even more simple: You just have to edit the item and then use SubmitChanges() to commit them.
As you can see, using LINQ allows you to write simple, readable code and much faster than if you had used the SharePoint API at that.
- Using LINQ to SharePoint [MSDN]