Re: LINQ - Speed penalty/anonymous types
Thanks for your comment.
It's just sample code - i'm not using it anywhere.
/Peter
>
> No. It's anonymous at compile time, but it's a perfectly ordinary type
> as far as the CLR is concerned. All you're doing in the above is
> calling a constructor and then retrieving properties.
>
> Of course, in the above case you could just go directly to the members
> of the list, but I'm assuming in real life that you actually have a
> reason to use the anonymous type in the first place.
>
> --
> Jon Skeet - <skeet [at] 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
Re: LINQ - Speed penalty/anonymous types
In this case, with a list - it would be *slightly* more efficient to just
access each item in the list, since it avoids creating a short-lived object
per row - i.e.
foreach(var originalItem in list) {
// do something with originalItem.ClassID etc
}
However, if "list" is actually an IQueryable (or similar) source such as an
LINQ-to-SQL DataContext, then your original (projection-based) code would be
more efficient: in this case, the provider can usually do something clever -
i.e. with SQL it can build a SELECT statement that only includes the two
columns, and doesn't worry about change-tracking etc.
Marc
Re: LINQ - Speed penalty/anonymous types
Thanks for your comment Marc.