Nov 07

As I”ve said in previous entries, I”m updating my ASP.NET 2.0 book to the new version. One of the controls that will be added to the platform is the LinqDataSource control. The control will let you perform any LINQ expression against any IEnumerable data source. Scott Guthrie has already a cool post that shows how to use the control over a LINQ to SQL model. What I wanted was to use the control against a collection of objects.

If you”ve used the control, you know that it requires you to set the ContextTypeName and TableName properties. My initial attempt consisted in building a class which had a method that returned a collection. It didn”t work so I tried to get some help from those that know (ie, the ASP.NET team – thanks again for the help guys! You”ve been amazing in these last months). My mistake was to assume that TableName expected a method (don”t ask me why, I just assumed that it had to be a method…). Nop, it really expects a property or field name.

With this new info, I”ve set up a small test that shows how to use the control without a LINQ to SQL model. Lets start with the simple classes I”m using:

public class Person
    private String _name;
    private String _address;

    public string Address
        get { return _address; }
        set { _address = value; }

    public string Name
        get { return _name; }
        set { _name = value; }

public class PersonManager
    private static readonly List<Person> _people = null;
    static PersonManager ()
        _people = new List<Person>
            new Person{ Name = “Luis”, Address=”Fx”},
            new Person{ Name = “Luis2″, Address=”Fx”},
            new Person{ Name = “Luis3″, Address=”Fx”},
            new Person{ Name = “Luis4″,Address=”Fx2”}

    public IList<Person> People
            return _people;

Now, we”ll use the PersonManager class to feed an existing LinqDataSource control:

<asp:LinqDataSource runat=”server”
               <asp:Parameter Name=”address” DefaultValue=”Fx” />

Isn”t this cool? I”ve  even tested the where expression and yes, it”s working! Ok, no the “not so cool part”: according to the team, insert, update and delete operations require that the ContextTypeName be a LINQ to SQL data context.

Anyway, now I know why this control wasn”t called “LinqToSqlDataSource” 🙂

2 comments so far

  1. John McFetridge
    6:12 pm - 1-16-2008

    This is great as the LinqDataSource is a wonderful RAD tool but useless in a Tiered application. Now we can return List from the DAL and still use the LinqDataSource.

  2. Vijay
    12:52 pm - 4-1-2009

    This is nice, quick and easy. Thanks for detailed explanation.