Switching from LinqToXYZ to LinqToObjects

Posted by spender on Stack Overflow See other posts from Stack Overflow or by spender
Published on 2010-03-25T01:42:08Z Indexed on 2010/03/25 1:53 UTC
Read the original article Hit count: 545

Filed under:
|

In answering this question, it got me thinking...

I often use this pattern:

collectionofsomestuff //here it's LinqToEntities
    .Select(something=>new{something.Name,something.SomeGuid})
    .ToArray() //From here on it's LinqToObjects
    .Select(s=>new SelectListItem()
        {
            Text = s.Name, 
            Value = s.SomeGuid.ToString(), 
            Selected = false
        })

Perhaps I'd split it over a couple of lines, but essentially, at the ToArray point, I'm effectively enumerating my query and storing the resulting sequence so that I can further process it with all the goodness of a full CLR to hand.

As I have no interest in any kind of manipulation of the intermediate list, I use ToArray over ToList as there's less overhead.

I do this all the time, but I wonder if there is a better pattern for this kind of problem?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ