XSL: for-each with max items

While working with Umbraco and Sitecore I learned some Xsl tricks.

The following example shows how to show the first 10 items in a HTML list.


<ul>

  

    &lt;xsl:if test=&quot;position()

      <li>

 <a href="{@link}">

   

  </a>

      </li>

    

  

</ul>


LINQ: Creating a if statement in Linq query

A lot of times I need to check a statement within my LINQ-query and I wish there was a possibility of a IF statement within LINQ.

The following code is the solution to my IF problem. I use a temporary variable (let isOlderThen30) to check if a statement is true. Then in my WHERE statement I use the temporary variable in a INFLINE IF.

For this example I use my Blogger class with some data



public class Blogger
{
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public int Age { get; set; }
 public string Blog { get; set; }
}

List personList = new List{
 new Blogger { FirstName = "Pieter", LastName = "Brinkman", Age = 27, Blog = "http://blog.newguid.net" },
 new Blogger { FirstName = "Mark", LastName = "van Aalst", Age = 26, Blog = "http://www.markvanaalst.com" },
 new Blogger { FirstName = "Bas", LastName = "Hammendorp", Age = 32, Blog = "http://www.hammendorp.net" }
};


It’s kind of hard to think of a easy good example, but here it is. In this example I want to set the Age property to “Older then 30” when the Blogger is older then 30 (how useful!).


//If a blogger is older then set Age text to "Older then 30"

var rawList = from item in personList
  let <strong>isOlderThen30</strong> = item.Age &gt; 30
  select new
  {
   Name = item.FirstName,
   Age = (isOlderThen30 ? "Older then 30" : item.Age.ToString()),
  };

//GENERATE OUTPUT

foreach (var item in rawList)
{
 Response.Write(item.Name + " (" + item.Age + ")<br />");
}

//OUTPUT
//Pieter (27)
//Mark (26)
//Bas (Older then 30)


Hope that the example is clear. If you have any questions let me know.

// UPDATE (3 aug 2009) //

I thought about the codeexample and offcourse you can do it shorter (but less readable with complex queries).


var rawList = from item in personList
  select new
  {
   Name = item.FirstName,
   Age = (item.Age &gt; 30 ? "Older then 30" : item.Age.ToString()),
  };


Linq to Xml: Generate Google Sitemap with sitemap-protocol

In this example I will generate a XML site-map that complies with the sitemap-protocol XML schema.


//create datasource

List blogPosts = new List{
 "http://blog.newguid.net/mypost1.aspx",
 "http://blog.newguid.net/mypost_about_Net.aspx",
 "http://blog.newguid.net/morePosts.aspx",
 "http://blog.newguid.net/andEvenMorePosts.aspx"
};

//Create namespace for sitemap-protocol

XNamespace xmlNS = "http://www.sitemaps.org/schemas/sitemap/0.9";
XDocument xmlDoc =
 new XDocument(
  new XDeclaration("1.0", "UTF-8", null),
  new XElement(xmlNS + "urlset",
   from blogPostUrl in blogPosts
   select
    new XElement(xmlNS + "url",
    new XElement(xmlNS + "loc", blogPostUrl))
    ));

//Show output

Response.Write(xmlDoc);


This example will give the following output:


&lt;urlset xmlns=&quot;<a href="http://www.sitemaps.org/schemas/sitemap/0.9">http://www.sitemaps.org/schemas/sitemap/0.9</a>"&gt;
  
    http://blog.newguid.net/mypost1.aspx
  
  
    http://blog.newguid.net/mypost_about_Net.aspx
  
  
    http://blog.newguid.net/morePosts.aspx
  
  
    http://blog.newguid.net/andEvenMorePosts.aspx
  



To keep the example as simple as possible I only use the LOC element of the URL node. In the real world you can implement the lastmod, changefreq and priority node.

More information about the sitemap-protocol.

Add meta data (keywords, description) dynamicly

Add meta-data dynamically to your page by adding a HtmlMeta control to your Header. In this example I dynamically add a keyword string to the page.


string keyWords = "metatags, html, dynamic, generate";

HtmlMeta keywords = new HtmlMeta();
keywords.Name = "keywords";
keywords.Content = keyWords;
Page.Header.Controls.Add(keywords); 


You can do the same for other meta-data like description.

Linq Casting: ToDictionary()

In this post I will give an example how to cast a GenericList to Dictionary.

This example will use the following Blogger class.


public class Blogger
{
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public int Age { get; set; }
 public string Blog { get; set; }
}


The example will cast a List<Blogger> to a dictionary with key FirstName, Lastname and value the Age of the blogger.


// DECLARE PERSONLIST

List personList = new List();
personList.Add(new Blogger { FirstName = "Pieter", LastName = "Brinkman", Age = 27, Blog = "http://blog.newguid.net" });
personList.Add(new Blogger { FirstName = "Mark", LastName = "van Aalst", Age = 26, Blog = "http://www.markvanaalst.com/" });
personList.Add(new Blogger { FirstName = "Bas", LastName = "Hammendorp", Age = 32, Blog = "http://www.hammendorp.net/" });

// CREATE NEW DICTIONARY FROM LIST
// with key FirstName + LastName and value Age

Dictionary AgeDictionary =
 personList.ToDictionary(x =&gt; x.FirstName + " " + x.LastName,
       x =&gt; x.Age,
       StringComparer.OrdinalIgnoreCase);

// GENERATE OUTPUT

foreach (KeyValuePair item in AgeDictionary)
 Response.Write("key: " + item.Key + " - value: " + item.Value + "<br />");

//// OUTPUT
//key: Pieter Brinkman - value: 27
//key: Mark van Aalst - value: 26
//key: Bas Hammendorp - value: 32


 Hope it helps.

Playing with JQuery (fixing Intellisense in VS2008)

The last few years I spend a lot of time working with Asp.Net AJAX. It all worked pretty good, the only downside is that you do not have control the generated HTML. With jQuery you can manipulate generated HTML. This HTML can be generated fully controlled with a ListView.

I'm a lazy programmer so the first thing I needed to do is fix jQuery Intellisense in VS2008. I found my information for doing this on Scott Gu's blog (jQuery Intellisense in Vs 2008). Everything worked great the only bad thing is that my computer needed a restart after installing a Visual Studio Patch…

Now the Intellisense is working the only thing I need to do is blog some nice examples of my work! 😉

Cheers,

Pieter