In this blog I will show you how to manipulate SharePoint objects with LINQ. In my personal opinion, LINQ makes code less verbose, hence makes code easier to read and understand. LINQ also makes SharePoint application development fun.
LINQ is general to Microsoft.NET framework and neither LINQ nor SharePoint are dependent on each other. Obviously SharePoint can be used without LINQ and similarly LINQ can be used outside of SharePoint with other .NET applications, but if you decide to use LINQ in your SharePoint applications and web parts then you would need to upgrade .NET framework runtime on your SharePoint farm to .NET 3.5 (ideally, to .NET 3.5 SP1). The assumption of this blog is that you are already familiar with basic concepts of LINQ. If you need to read more about LINQ then visit MSDN LINQ page. LINQ has many different flavors: LINQ to Objects, LINQ to SQL (already dying?), LINQ to Entities, LINQ to XML, and LINQ to Data set. In this blog, we will use LINQ to Objects to manipulate SharePoint objects, so let’s start. LINQ to Objects allow developers to query in-memory collections without writing a lot of loops and conditional statements.
So in this example, I have a SharePoint list called “States” that includes two columns: Title and Abbreviation.
Here is what I want to accomplish:
1. Show count of all states.
2. I want to show all states in ascending and descending order.
3. I want to show count of all states that start with I or N.
4. I want to show all states that ends with an A.
Number 1 is fairly easy to do without LINQ. SPListItemCollection has Count property that returns the total count of items in the collection.
Figure2: Count of all states without using LINQ
Number 2 is not very straight forward. SPListItemCollection has no Sort property or Sort method. Without LINQ you have to copy each SPListItem’s title in a sorted list or a list and the use default comparer to sort states ascending and descending. Similarly, with number 3 and 4, you have to loop through each item and check title property (string) to see if it starts with I or N (number 3) or ends with an A (number 4). This is not very hard but it is a lot of code to do something fairly simple
With LINQ you can use lambda expressions to specify your condition and it takes care of the rest. Listing 3 shows a LINQ example to do 1 through 4. Notice, the foreach statements are used to write output to console application only and code contains no conditional statements (other than LINQ conditions).
The code in the example uses three main .NET 3.5 features:
- .NET Extension Methods (See my blog on Extension Method)
- .NET Generics
.NET Generics have been around since .NET 2.0 and LINQ heavily relies on them. LINQ only works with iEnumerable<T> collections and all legacy IEnumerable collections must be converted to IEnumerable<T> collections. For conversion two methods are available: Cast method and OfType method. The OfType method returns only those elements in source that can be cast to type provided. To instead receive an exception if an element cannot be cast to type provided, use Cast method. - Lambda Expressions
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
The code snippet below takes a SharePointListItemCollection and converts it into IEnumerable<SPListItem> collection
// Convert SharePoint Legacy Collection to a .NET generic collection
IEnumerable<SPListItem> states = stateList.Items.OfType<SPListItem>();
The code snippet below uses lambda expression and “Where” extension method to define query condition.
// All states that end with an A
states.Where(state => state.Title.EndsWith("A")
One important thing to keep in mind is that when you are working with LINQ to Objects, you are working with objects in memory. LINQ doesn’t replace your standard SharePoint CAML queries; it provides you an additional approach to quickly manipulate already fetched data from the server. If you are interested to use LINQ to replace CAML , you should look at the popular LINQ to SharePoint CodePlex project. The LINQ to SharePoint project provides a custom query provider for LINQ that allows to query SharePoint lists using familiar LINQ syntax.
I hope this post provided you enough information to jump start your LINQ projects in SharePoint.
Talha