in SharePoint

Simple way to use LINQ with SharePoint ItemCollections

Here is a simple way to make use of LINQ with SharePoint item collections, for example, with List Items

SPListItemCollection listPages = pagesList.Items;
var latestPages = listPages.Cast<SPListItem>().Where(p => p.Name != defaultPageName);

And this becomes really cool if you use with Dynamic LINQ

var latestPages = listPages.Cast<SPListItem>()
                                .Where(p => p.Name != defaultPageName)
                                .OrderByDescending(p => p[Constants.Modified].ToString())
                                .Take(3);

How cool is that? :)

Write a Comment

Comment


9 + = thirteen