(This would be better off in the C# group, btw - the CLR is unaware of
anonymous types.)
> I am trying to declare a container and then add anonymous types to it. I
> want to end up with something I can use with LINQ, but the normal way to load
[quoted text clipped - 9 lines]
>
> I need a new pattern for LINQ.
You can't declare the target container ahead of time in that way. Two
options:
1) Use LINQ queries instead of manually adding things to the list. For
instance:
var list = someList.Select(x => new { Name = x, Company = aComp })
.ToList();
2) Create the list using type inference and a generic method which just
creates an empty list, e.g.
var list = CreateEmptyList(new { Name="Dummy", Company="Dummy" };
...
List<T> CreateEmptyList<T> (T sampleElement)
{
return new List<T>();
}
Personally I'd prefer the first route.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
ChrisA - 31 Dec 2007 18:16 GMT
Jon,
Sorry about posting to the wrong area. I didn't realize anonymous types
weren't in the CLR.
I think I can get your first suggestion to work. Is it possible to
declare a variable inside the lambda? I need something like:
(x =>
string[] fields = x.Split();
new { name = fields[0], company = fields[1];
)
but I'm getting compiler errors.
I appologize for taking up your time -- I clearly have a lot of reading to
do before I've got this down.
Thanks
ChrisA - 31 Dec 2007 18:33 GMT
Jon,
Got it - just needed the brackets.
Thanks for the help. The new 3.5 features are even better than I
expected, now that I'm starting to build real code.
Jon Skeet [C# MVP] - 01 Jan 2008 15:06 GMT
> Sorry about posting to the wrong area. I didn't realize anonymous types
> weren't in the CLR.
No problem :) It's often not made terribly clear which things are
language features, which are framework (library features) and which are
CLR features. The CLR for .NET 3.5 is the same as for .NET 2.0.
> I think I can get your first suggestion to work. Is it possible to
> declare a variable inside the lambda? I need something like:
[quoted text clipped - 6 lines]
> I appologize for taking up your time -- I clearly have a lot of reading to
> do before I've got this down.
Can I give a quick shameless plug?
http://manning.com/skeet
And there's no need to apologise - if I didn't want to take time
helping people, I wouldn't be here :)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk