Metalogix SharePoint Site Migration Manager

SharePoint 2010 Cookbook: Using LINQ to Query SharePoint Lists

Published 05/24 courtesy of Bamboo Solutions Community

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:

Read more



Recent SharePoint Questions

more sharepoint questions


More Articles By

Develop Mobile Applications for SharePoint with Mobile Entree - CMSWire


Develop Mobile Applications for SharePoint with Mobile Entree
CMSWire, CA
By Barb Mosher | Jun 5, 2009 Seeing as how SharePoint (news, site) is so widely used within the enterprise today, it's…

Read more

Bamboos Year in Review: Marc OBrien Introduces the Bamboo Online Applications Division

Editor's note:  Last year we introduced the Bamboo Year in Review feature, kicking off with a note

Read more

Working with the Admin Links on your SharePoint Blog

While writing the final sentences of my post on how to create a SharePoint blog last week, I realized that I needed to circle back and spend some time…

Read more

More Articles Under "News from Around the Web"

Guest Blog by H3 Solutions Jason Hall - Mobile Entrée, Taking a Look Under the Hood

Mobile Entrée is installed as a SharePoint solution and is deployed as a series of features. 

Read more

SharePoint on Your SmartPhone, Android Moves to Laptops, Best Practices Conference Speakers List

Top News Stories
Google Wave - A Developer's Eye View (The Register)
Last week, Google announced Wave, a…

Read more

Announcing the Best Practices Conference Speakers List!

Make your plans now to attend the Best Practices Conference this August 24-26 in Washington, D.C. to ensure that you don't miss out on sessions presented by some…

Read more



Metalogix Selective Resore Manager Pro