[Journal - Tuples and Projection]

Tuples and Projection

Sunday, February 19, 2006

Suppose you have a list of people:

IList<Person> people = new List<Person>();
people.Add(new Person("John", "Doe", "Admin"));
people.Add(new Person("John", "Doe", "Cook"));
people.Add(new Person("Sue", "Foo", "Programmer"));
people.Add(new Person("Sue", "Foo", "FlagWalker"));

And you want to select just the first and last names from it, in order to further restrict the list to distinct combinations of first and last name:

select distinct FirstName, LastName from People;

In Orcas, you'll be able to say:

var names = from p in people
            select new { p.FirstName, p.LastName };

var distinctNames = names.Distinct();

In the meantime, we'll do without the syntax sugar:

IList<CTuple<string, string>> names
  =
    Walk.Select<Person, CTuple<string, string>>
    (
        people,
        delegate(Person p)
        {
            return new CTuple<string, string>(p.FirstName, p.LastName);
        }
    );

IList<CTuple<string, string>> distinctNames
  =
    Walk.Distinct<CTuple<string, string>>(names);

We don't have anonymous tuple types yet, but we can define a set of generically overloaded types. Other differences are the lack of SQL-like syntax, type inference for locals, and extension methods.