Switching from LinqToXYZ to LinqToObjects
- by spender
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?