Convert an SPListItemCollection to an IEnumerable

By adminNo Comments

In one of my last project I make a function to accept an IEnumerable<T> as parameter. I normally used this function with PublishingPageCollection. This collection inherits fromĀ QueryBasedCollection<PublishingPage> and so it is a IEnumerable. Unfortunately the SPListItemCollection it is not.

I didn’t want to change my method and I didn’t want to create an overload only for the type of parameters. Then…I remembered that LINQ is my friend :)
You can directly convert an SPListItemCollection. Below an example…hope can help you…

using (SPWeb web = SPContext.Current.Site.OpenWeb("myweb"))
{
       return web.Lists["MyList"].Items.Cast<SPListItem>().Select(item => item);
}
SharePoint

Leave your Comment

Your email address will not be published. Required fields are marked *

*


* 5 = twenty five

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Blue Taste Theme created by Jabox