The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,551,710
since: 19 Jan 2005

LINQ to SharePoint - Its magically delicious!

posted Wed 11 Oct 06

As I was writing code for a chapter last night, I thought to myself: "Self, it would be really cool if you could use LINQ to manipulate the SharePoint object model instead of all these klunky old loops." Well, thats exactly right. Since LINQ queries work on any in-memory object that implements IEnumerable, robust (but often difficult to use) object models are already exposed to LINQ and ready to be improved.

First, let's take a look at some sample code that builds a List of SPList instances where the list is hidden on a given site:

SPSite site = new SPSite("http://server/site");
SPWeb web = site.AllWebs[0];

List<SPList> hiddenLists = new List<SPlist>(); // Do not use an SPListCollection here
foreach (SPList list in web.Lists)
{
    if (list.Hidden)
        hiddenLists.Add(list);
}

This doesn't look that bad... But if you have been using LINQ at all, you know you can do this in one very easy to read line of code:

var hiddenLists = from list in web.Lists where list.Hidden select list;

Doesn't that look and feel much better? I think so. You could even make your own generic, flexible lambda function that can be used as a filter mechanism for lists. If you want more information on lambda expressions, check out my blog entry, Lambda Lambda Lambda

What if you wanted to sort the lists in descending order by title? Usually, on in-memory structures this is either a pain in the butt, or just never looks good. With LINQ, its ridiculously easy:

var hiddenLists = from list in web.Lists orderby list.Title descending where list.Hidden select list; 

Let's say you plan on accessing the hidden list collection a lot. What if you then also plan on performing queries on the hidden list. In this case, it doesn't really make sense to generate the hidden list collection using a standard query, since you're going to run a query off that and nested queries can get difficult to use. What if you decided to make a language extension to the SPListCollection class that simply returned the hidden items? Now we're talkin:

static class SPListCollectionExtender
{
    public static List<SPList> AllHidden(this SPListCollection input)
    {
       return
         (from list in input where list.Hidden select list).ToList();
    }
}

With that in place, you can then display the titles of all hidden lists in a given SPListCollection class like this:

foreach (SPList hiddenList in web.Lists.AllHidden) 
{
    Console.WriteLine("{0}", hiddenList.Title);
}

Programming with LINQ in mind often requires a whole new way of thinking. However, I strongly recommend that everyone start trying to think the "LINQ" way now.. As you can see, with just a few simple tweaks here and there, the cleanliness and efficiency of your code can be drastically improved - without even touching on any of the additional functionality coming up like the ADO.NET Entity Framework. 

tags:            

links: digg this    del.icio.us    technorati    reddit




1. Steve left...
Wed 11 Oct 06 8:29 am :: http://www.sharepointbuzz.com

You are now part of the BUZZ, Sharepoint BUZZ

Visit http://www.sharepointbuzz.com


2. Craig left...
Sun 22 Oct 06 9:22 pm

you know just had a nasty thought about the entity framework ... like almost all MS prebuilt technologies will this only work with static connection strings? my connection strings are not in my web.config, they are dynamic and loaded based on the site being requested .... I hope this is supported :) it will suck if you can't work like this ... membership is another area where you can't overload the connection string objects.


3. Alex James left...
Tue 28 Nov 06 3:58 am

Kevin, I don't know a lot about Sharepoint, but I assume that those lists can have a lot of data in them, so I highly recommend that you don't use LINQ like you are suggesting.

What you are doing in this line: var hiddenLists = from list in web.Lists where list.Hidden select list;

is bringing every list into memory and then filtering out those that aren't hidden, that is kind of wasteful.

What you really want is something that implements IQueryable<T> for Sharepoint. In which case you would be passed an ExpressionTree, which you can convert into appropriate calls so only the necessary list are brought into memory in the first place.

This is what DLINQ and VLINQ (ADO.NET Orcas) will do, and it what anyone who wants to provide a truly integrated and optimized LINQ experience should do for their datasource.

I am sure it won't be long before we see: GLINQ (LINQ for Google), RSSLINQ (you can guess) etc... Sharepoint seems like another good option, although it does sort of rely on the SP api providing the ability to filter before you get the results!


4. Bart De Smet left...
Mon 16 Jul 07 11:05 pm :: http://blogs.bartdesmet.net/bart

You might be interested in the LINQ to SharePoint project on CodePlex too. The goal of the project is to create a custom LINQ provider that supports translation of LINQ queries into CAML queries for SharePoint lists, together with an entity model.

For more info, see www.codeplex.com/LINQtoSharePoint


5. Scott Brwn left...
Tue 04 Dec 07 12:17 am :: http://www.codeplex.com/glinq

check out my Linq to Google on codeplex http://www.codeplex.com/glinq


Tag Related Posts

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

CLINQ v1.1.0.0 Released!

Fri 02 May 08 5:38 P GMT-05
tags:          

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Continuous LINQ v1.0.0.0 Released

Tue 08 Jan 08 4:53 P GMT-05

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

Unexpected and Unsafe behavior in LINQ

Wed 14 Nov 07 8:09 P GMT-05
tags:    

November is National Novel Writing Month!

Thu 01 Nov 07 12:42 P GMT-05

On Writing

Thu 13 Sep 07 1:44 P GMT-05

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

Continuous LINQ

Mon 06 Aug 07 1:21 P GMT-05
tags:    

Dynamic, Observable LINQ Views

Tue 31 Jul 07 1:21 A GMT-05
tags:        

I got on the endcap baby!

Sun 10 Jun 07 1:06 A GMT-05
tags:      

Installing Orcas Beta 1 - VMware Style

Mon 23 Apr 07 12:16 P GMT-05

Core Data - Almost too Easy?

Wed 18 Apr 07 2:23 P GMT-05

Will Silverlight be DOA?

Mon 16 Apr 07 8:02 P GMT-05

My Little Pony .NET Unleashed 2007

Fri 30 Mar 07 1:59 P GMT-05

An experience with the Leopard beta

Mon 26 Mar 07 7:45 P GMT-05
tags:                

Authorness

Thu 15 Mar 07 1:44 P GMT-05

Visual Studio "Orcas" - March CTP is Available

Wed 28 Feb 07 12:28 P GMT-05
tags:            

Objective-C Categories vs C# 3.5 Language Extensions

Mon 26 Feb 07 1:05 P GMT-05
tags:                

DLinq vs the ADO.NET Entity Framework

Fri 23 Jun 06 4:01 P GMT-05

Tech-Ed 2006 Day 1 - Registration Day

Sun 11 Jun 06 7:17 P GMT-05