Using LINQ to SharePoint

Applies to: SharePoint Foundation 2010

This article provides an overview of the LINQ to SharePoint provider, which enables you to use Language Integrated Query (LINQ) queries in your Microsoft SharePoint Foundation solutions. The provider also enables changing data using optimistic concurrency. Object change tracking and resolution of conflicts is also supported by LINQ to SharePoint.

LINQ and LINQ Providers

LINQ is a feature of the programming languages C# and Microsoft Visual Basic .NET. Compilers are included with Visual Studio.

LINQ adds a SQL-like syntax and vocabulary to each of the languages, which can be used to query data sources. But unlike other languages and query syntaxes which vary from one type of data source to another, LINQ can be used to query, in principle, any data source whatsoever. For this reason, developers may find that it is the only query syntax that they ever need to know.

All that is necessary to make a data source accessible with LINQ is that someone create a LINQ provider for the data source. A LINQ provider is an implementation of the System.Linq.IQueryable<T> and System.Linq.IQueryProvider interfaces that are included with Microsoft .NET Framework (System.Core.dll). The implementing classes must be public in a managed code assembly. The primary job of the class that implements IQueryProvider is to translate LINQ queries into the language of the data source, such as SQL or XQuery, then call the data source’s application to execute the query.

The provider must also expose a gateway class whose instances can communicate with the data source and output IEnumerable<T> objects. For example, the gateway class for LINQ to SQL is DataContext and the gateway class for LINQ to XML is XDocument. The gateway class must implement System.Linq.IQueryable<T> or have a child property that does so or have a method that returns a type that implements System.Linq.IQueryable<T>. For example, DataContext has a GetTable() method that returns a Table<TEntity> type that implements System.Linq.IQueryable<T>. The latter interface, in turn, has a property of type IQueryProvider. (The gateway class can also directly implement IQueryProvider.) It is objects of type Table<TEntity> that are queried.

In many cases, a LINQ Provider cannot be used by a .NET solution developer unless the developer creates a set of entity classes to represent the subordinate entities in the data source, such as the particular tables of a particular SQL database. Frequently, a lot of such classes must be created (e.g., a database with three dozen tables), so the developer of a LINQ provider will typically include a code generation tool to automate the process of creating these entity classes.

Learning about LINQ

Before working with the SharePoint to LINQ Provider, developers should learn about LINQ in general and how it is used with the LINQ to SQL provider and the special LINQ to Objects provider that are included with .NET Framework.

Microsoft Developer Network (MSDN) has a great deal of information, including some videos, about LINQ and the providers built into .NET Framework. There are also many books on the subject.

The LINQ to SharePoint Provider

The LINQ to SharePoint Provider is defined in the Microsoft.SharePoint.Linq namespace. It translates LINQ queries into Collaborative Application Markup Language (CAML) queries. It is no longer necessary for developers to know how to write CAML queries. LINQ queries can be used in server code. To query from a client application, use SharePoint’s support for ADO.NET Data Services.

The gateway class for the LINQ to SharePoint provider is Microsoft.SharePoint.Linq.DataContext which represents the data of a SharePoint Foundation Web site. It is parallel in use and function to the System.Data.Linq.DataContext class in the LINQ to SQL provider. Just as the latter class has a GetTable() method that returns a Table<TEntity> object that implements System.Linq.IQueryable<T>, so too, the Microsoft.SharePoint.Linq.DataContext class has a GetList<T> method that returns an EntityList<TEntity> class that implements System.Linq.IQueryable<T>. It is objects of type EntityList<TEntity> that are queried.

The following is an example of the use of LINQ to query SharePoint Foundation.

// Get DataContext from page context
DataContext data = new DataContext(SPContext.Current.Web.Url);

// Get the SharePoint list
EntityList<Customer> Customers = data.GetList<Customer>("Customers");

// Query for customers from London
var londonCustomers = from customer in Customers
                      where customer.City == "London"
                      select customer;

foreach (var londonCust in londonCustomers)
{
    Console.Writeline("id = {0}, City = {1}", 
                      londonCust.CustomerId, 
                      londonCust.City);
}

For more information about querying with LINQ to SharePoint, see How to: Query Using LINQ to SharePoint.

The following is an example of using LINQ to add an item to a SharePoint Foundation list.

// Get DataContext from page context
DataContext data = new DataContext(SPContext.Current.Web.Url);

// Get the SharePoint list
EntityList<Customer> Customers = data.GetList<Customer>("Customers");

// Create the item to be added
Customer newCustomer = new Customer() { CustomerId=36, City=”Madrid” };

// Mark the item to be added on the next call of Submit
Customers.InsertOnSubmit(newCustomer);

// Submit all changes
data.SubmitChanges();

For more information about adding, editing, and deleting list items with LINQ to SharePoint, see How to: Write to Lists Using LINQ to SharePoint.

See Also

Reference

Microsoft.SharePoint.Linq.DataContext

EntityList<TEntity>

GetList<T>

IEnumerable<T>

System.Linq.IQueryable<T>

IQueryProvider

Microsoft.SharePoint.Linq

Other Resources

LINQ to Objects

LINQ to SQL: .NET Language-Integrated Query for Relational Data

LINQ to XML