HOWTO: Order generic collections with LINQ

To order collections in LINQ you can use the extension method OrderBy(), which can be found in the static class Sytem.Linq.Enumerable. It can be used in all types of collections that implement the interface System.Collections.Generic.IEnumerable. The following example orders a list of Person objects by their age.


List persons = new List();

persons.Add(new Person("Peggy", "Bundy", 39));
persons.Add(new Person("Doug", "Heffernan", 34));
persons.Add(new Person("Tim", "Tailor", 41));
persons.Add(new Person("Homer", "Simpson", 34));
persons.Add(new Person("Al", "Bundy", 41));

persons = persons.OrderBy(person => person.Age).ToList();

Output:
Heffernan, Doug (34)
Simpson, Homer (34)
Bundy, Peggy (39)
Tailor, Tim (41)
Bundy, Al (41)

The person class is quite simple:


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

    public Person(string firstName, string lastName, int age)
    {
        FirstName = firstName;
        LastName = lastName;
        Age = age;
    }

    public override string ToString()
    {
        return string.Format("{0}, {1} ({2})", LastName, FirstName, Age);
    }
}



If you want to order the persons also by their last name you can use the extension method ThenBy(). It has to be appended at the end of the OrderBy() method.


persons = persons.OrderBy(person => person.Age).ThenBy(person => person.LastName).ToList();

Output:

Heffernan, Doug (34)
Simpson, Homer (34)
Bundy, Peggy (39)
Bundy, Al (41)
Tailor, Tim (41)

Infragistics WebHierarchicalDataGrid: “theForm is undefined”

I am using Infragistics WebHierarchicalDataGrid inside my webapplications sometimes. After I deployed one of those applications onto the customers web server (Windows Server 2003 with IIS 6) I got the error mentioned above. I was wondering why, because on my development machine (Windows 7 with installed IIS) everything works fine. Finally renaming the form from “form1” to “theForm” did the trick.


<form id="theForm" runat="server">

Setzen eines Properties per Reflection

Ab und an kann es nützlich sein, Properties einer Klasse per Reflection setzen zu können. Dazu ist lediglich folgender Code nötig:


public void SetProperty( string nameOfProperty, object value )
{
    PropertyInfo propertyInfo = GetType().GetProperty( nameOfProperty, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase );
            
    if( propertyInfo == null )
        throw new NullReferenceException( string.Format( "Die Eigenschaft {0} konnte nicht gefunden werden!", nameOfProperty ) );

    Type propertyType = propertyInfo.PropertyType;
    propertyInfo.SetValue( this, Convert.ChangeType(value, propertyType), null );
}

Beispiel:


public class Person
{
    public string Vorname { get; set; }
    public string Nachname { get; set; }

    public override string ToString()
    {
        return string.Format("{0}, {1}", Nachname, Vorname);
    }

    public void SetProperty( string nameOfProperty, object value )
    {
        PropertyInfo propertyInfo = GetType().GetProperty( nameOfProperty, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase );
            
        if( propertyInfo == null )
            throw new NullReferenceException( string.Format( "Die Eigenschaft {0} konnte nicht gefunden werden!", nameOfProperty ) );

        Type propertyType = propertyInfo.PropertyType;
        propertyInfo.SetValue( this, Convert.ChangeType(value, propertyType), null );
    }
}


class Program
{
    static void Main(string[] args)
    {
        Person person = new Person();
        person.SetProperty("Vorname", "Thorsten");
        person.SetProperty("Nachname", "Vogt");
        Console.WriteLine(person); // Ausgabe: Vogt, Thorsten
    }
}

Public Key Token einer Assembly ermitteln

Um den Public Key Token einer signierten Assembly zu ermitteln, kann das Tool sn.exe genutzt werden. Hierzu einfach den Parameter -T mit angeben und die entsprechende Assembly an den Befehl anhängen. Soll der entsprechende Public Key der Assembly mit ausgegeben werden, so ist der Parameter -Tp zu verwenden.

sn -Tp MyAssembly.dll
Microsoft (R) .NET Framework Strong Name Utility Version 3.5.30729.1
Copyright (c) Microsoft Corporation. All rights reserved.

Public key is
00240000048000009400000006020000002400005253413100040000010001007b625679a9fee3
f6c3c6fa478d4fbcbf46148d0691210aa88aeaec4ff4d350e2261a49b261227105e9257503dc69
51c417e0b0cc924f0c53afa7815499c4c3bc3321547b6f883f75c42b88bcf0ac39690c0f8b3d42
2cc3e5e93f3528e15c32aedf70b930cd75a0ee1f0c87d1f8b35a633d3d002afc566191175d7c5d
90f1fdb2

Public key token is 2bb82c850a7b28d3

Ich habe noch einen Blogeintrag gefunden, der die Einrichtung in Visual Studio beschreibt.