Sunday, November 8, 2009

Linq to SQL Partial Classes

While experimenting with Linq to SQL, I realized right away that I needed to extend the functionality of the entity classes generated by the Linq to SQL designer.  I wanted to be able to sort the entity classes by having them implement IComparable.

The solution turned out to be simple:  extend each entity class by defining a partial class, and implement IComparable in the partial class definition.  For example, I had an entity class Newspaper, which I extended with a partial class:

public partial class Newspaper : IComparable<Newspaper>
{
#region IComparable<Newspaper> Members
public int CompareTo(Newspaper other)
{
if (other == null)
throw new ArgumentNullException("other");
return Newspaper_Name.CompareTo(other.Newspaper_Name);
}
#endregion
}



2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello,

    You should check out plinqo.com. It extends Linq to SQL and provides many enhancements like one class per file, rules, auditing and much more.

    Thanks
    -Blake Niemyjski

    ReplyDelete