Technical Fun

Archive for the ‘.Net’ Category

This one’s a sequel to my last post about LINQ-ize a SPListCollection.
I got another not-so-important urge to get a maximum number from one of my custom list’s field.
Let’s just say the the field containing the number is “Document Number”, and I want to grab the current maximum value of that field to do various purposes.

Now, as I am a little bit obsessed with Linq, so let’s leave CAML behind and grab Linq instead.
Let’s begin by creating a wrapper around SPListItemCollection object to make it Linq-able (just like I did with the SPListCollection).

public class SPListItemCollectionAdapter : List<SPListItem>
private SPListItemCollection _listItemCollection;

public SPListItemCollectionAdapter(SPListItemCollection listItemCollection)
_listItemCollection = listItemCollection;


private void Refresh()

foreach (SPListItem item in _listItemCollection)


then voila!, you can use Linq’s aggregation function to grab the max value currently on a field called “Document Number” inside a list called “Document”. Write a method that might look like this:

private static double GetMaxFrom(string listname, string fieldname)
SPListItemCollection itemcol = mossWeb.Lists[listname].Items;
SPListItemCollectionAdapter itemsAdapter = new SPListItemCollectionAdapter(itemcol);
var result = itemsAdapter.Max(x => x[fieldname]);
return (double)result;

mossWeb is just a singleton for the SPWeb object, as I was using it numerous times on other methods as well.
I also took the liberty of using the lambda expression (x => x[fieldname]) just to evaluate the specific field.
Using it is as easy as calling GetMaxFrom("Document","Document Number");


After trying this LINQToSharePoint, I must admit I was a bit carried away in forcing Linq to my SharePoint programming needs.
I had this one very “not so important” ambition of iterating through SPList object within the SPListCollection on a SPWeb object.
Problem popped out, SPListCollection was not implementing IEnumerable, so I could not walk easily using Linq to this collection.
So.. to fulfill my ambition, I created a pseudo-adapter class which looked like this:

public class SPListCollectionAdapter : List<SPList>
private SPListCollection _listCol;

public SPListCollectionAdapter(SPListCollection listCol)
_listCol = listCol;


private void Refresh()

foreach (SPList item in _listCol)

and shortly after that, I could then satisfy my quaint hunger with this method:

private static void DoMOSSLinq()
SPSite site = new SPSite(“http://titanctp2:9000&#8221;);
SPWeb web = site.AllWebs[0];

SPListCollectionAdapter listAdapter = new SPListCollectionAdapter(web.Lists);

var result = from l in listAdapter
select l;

foreach (var i in result)


woohooo!!! I am no longer hungry.

I got an XML document that looks like this.

<Area id=”1″>
<name>Brave Land</name>
<description>An area where the brave may live forever</description>

Now, if I want to search and fetch the text inside the “name” tag based on a supplied “id”, I can rely faithfully on LINQ.

XDocument doc = XDocument.Load(PATH_TO_XML_DOCUMENT);
var x = from area in doc.Descendants(“Area”)
          where (int)area.Attribute(“id”) == MY_SUPPLIED_ID
          select (string)area.Element(“name”);

and consume it using x.First()

aaah …. how readable, how lovely, Nirvana!!!

Once upon a time, a narcissist felt the urge to display his assembly version to the crowd.
He had edited the AssemblyInfo.cs’ AssemblyVersion attribute to use 1.0.0.*.
Now, he wanted everyone to see the strange numbers of his assembly version.

He referenced System.Reflection carefully,
He then called these lines solemnly

Assembly asm = Assembly.GetCallingAssembly();
string version = asm.GetName().Version.ToString();

He chuckled in an insane glee, and disappeared, never again seen.
He had reached the Nirvana.

Tags: ,