SharePoint 2010 Cookbook: Using LINQ to Query SharePoint Lists

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:

SPList productsList = SPContext.Current.Web.Lists[“Products”];


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:

SPList productsList = SPContext.Current.Web.Lists[“Products”];


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:


SharePoint

Applications

SharePoint apps are stand-alone applications that perform specific tasks on a SharePoint site. Apps can perform functions such as managing a discussion board or knowledge base, performing project management or time tracking tasks, or doing other workflow operations.

SharePoint

Product Suites

Experience greater power and savings by bundling our SharePoint apps and web parts.


Essentials


Essentials Plus


Bamboo Premier


Project Management Suite


Knowledge Management Suite


External User Manager


SharePoint

Web Parts

Extend SharePoint beyond its out-of-the-box capabilities by tailoring it to your requirements with Bamboo Solution’s growing portfolio of Web Parts. Web Parts are the building blocks of pages on a SharePoint site that can be used to customize the user interface and content of a site page. 

SharePoint

Product Suites

Experience greater power and savings by bundling our SharePoint apps and web parts.


Essentials


Essentials Plus


Bamboo Premier


Project Management Suite


Knowledge Management Suite


External User Manager


Office 365

Cloud Parts

Cloud Parts are functional components that extend your SharePoint environment whether it’s hosted, on-premises, or part of Microsoft Office 365. More than mere ports of existing software to the cloud, our Cloud Parts have been built from the ground up to take advantage of the best that the cloud has to offer.

SharePoint

Product Suites

Experience greater power and savings by bundling our SharePoint apps and web parts.


Cloud Parts Suite for O365/SP Onl.


Featured Services

SharePoint Health Check

A SharePoint Health Check will identify the causes of issues and risks associated with your specific environment, and is custom tailored to provide you with the best recommendations to optimize your SharePoint environment.

SQL Health Check

Document recommendations relating to performance, stability, availability, or a specific focus you request of your SQL Server database instances.

My SharePointXperts

The truth is that each SharePoint skill may not be a full time job for many organizations, and it is nearly impossible for one person to do everything you need – so augment your team with SharePointXperts; providing the skill sets you need when you need them!