I think I'm starting to traverse the Linq/Orcas circle of (software) life, because I finally hit something that I don't like and that I couldn't work around. So far, anyway - maybe someone out there can tell me where I'm going wrong.
You may know that in the Orcas version of C#, they've added support for initializing collections via the funky new object initialization syntax. You can read about it here. The issue that I'm running into has to do with the fact that I'd like to initialize a collection in an object initialization expression, but I'm using XLinq to do it, and as a result I'm getting a compiler error.
See, I can get this to work:
Person craig = new Person
{
Name = "Craig",
Age = 35
Children =
{
new Person
{
Name = "Ellen",
Age = 2.9
},
new Person
{
Name = "TBD",
Age = -0.4
}
}
}
But because this is just a simple example and in my real code I don't know the collection items at compile time, what I really want to do something is like this:
Person craig = new Person
{
Name = "Craig",
Age = 35,
Children =
{
GetProgeny("Craig Andera")
}
}
Where the Person class is the obvious implementation, and GetProgeny returns IEnumerable<Person>. Without doing anything else, I get a compiler error saying that Collection<Person> does not contain an overload of Add that accepts an IEnumerable<Person>, which makes perfect sense given that the collection initialization syntax is just shorthand for a bunch of calls to Add.
The part that sucks is that I can't get this to compose with extension methods to get what I want. That is, I'd like it if I were able to define an extension method like this:
public static void Add(this ICollection<Person> collection, IEnumerable<Person> items)
{
foreach (Person item in items)
{
collection.Add(item);
}
}
But that doesn't work. I'm not sure why, but here's the list of things I hope it is, in decreasing order of desirability:
- I'm doing something wrong.
- It doesn't work and there's a good reason.
- It doesn't work and there's no good reason.
Anyone have any idea which it is?
Update: I should have said right up front that Children is a read-only property of Person. I always make my collections read-only, and would prefer to do so in this case as well.