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.
Challenge:
How can I use LINQ to query data from SharePoint Lists in 2010?
Solution:
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:
IEnumerable<SPListItem> products = productsList.Items.OfType<SPListItem>();
var results = from prod in products where prod.Title.Contains(“linq”) select prod;
or, if you prefer lambda expressions:
IEnumerable<SPListItem> products = productsList.Items.OfType<SPListItem>();
var results = products.Where(prod => prod.Title.Contains(“computer”));
foreach (SPListItem item in customers)
{
//doing something here.
}
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.
// Get DataContext from page context
DataContext data = new DataContext(SPContext.Current.Web.Url);
// Get SharePoint list
EntityList<Customer> Customers = data.GetList<Customer>("Customers");
// Query for customers from Reston
var restonCustomers = from customer in Customers
where customer.City == "New York"
select customer;
foreach (var cust in restonCustomers)
{
// Doing something here.
}
Another example, to add an item to the List:
// Get DataContext from page context
DataContext data = new DataContext(SPContext.Current.Web.Url);
// Get SharePoint list
EntityList<Customer> Customers = data.GetList<Customer>("Customers");
// Create the item to be added
Customer newCustomer = new Customer() { CustomerId=1, City="John"};
// Mark the item to be added on the next call of Submit
Customers.InsertOnSubmit(newCustomer);
// Submit all changes
data.SubmitChanges();
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.
See Also:
- Using LINQ to SharePoint [MSDN]