Why is my domain to dto converter called twice?
I encounted a strange behavior in my wcf service. This is my opreation contract implementation:
public IEnumerable<Dto.ProductType> GetAllProductTypes()
{
return m_InventoryRepository.GetAllProductTypes().Select(DomainToDtoConverter.ConvertToDto);
开发者_如何学Python}
If I have let say 3000 product types, then I should expect that my domain to dto converter is called 3000 times. But this is not the case; it is called 6000 times. If I then ensure that I load all the items before I return like this:
...Select(DomainToDtoConverter.ConvertToDto).ToList();
...then the converter is called the 3000 times that I would expect.
Anyone know why this happens?
I guess we will change all our operation contracts to return an array instead of IEnumerable
LINQ methods use lazy evaluation; the method runs again each time you iterate the results.
Calling ToList()
forces it to create a collection containing all of the results, allowing you to iterate that collection without re-executing anything.
精彩评论