Iteration over a linq to sql query is very slow.

Posted by devzero on Stack Overflow See other posts from Stack Overflow or by devzero
Published on 2010-06-16T10:17:16Z Indexed on 2010/06/16 10:22 UTC
Read the original article Hit count: 193

Filed under:
|

I have a view, AdvertView in my database, this view is a simple join between some tables (advert, customer, properties). Then I have a simple linq query to fetch all adverts for a customer:

public IEnumerable<AdvertView> GetAdvertForCustomerID(int customerID)
{
    var advertList = 
        from advert in _dbContext.AdvertViews
        where advert.Customer_ID.Equals(customerID)
        select advert;
    return advertList;
}

I then wish to map this to modelItems for my MVC application:

public List<AdvertModelItem> GetAdvertsByCustomer(int customerId)
{
    List<AdvertModelItem> lstAdverts = new List<AdvertModelItem>();
    List<AdvertView> adViews = _dataHandler.GetAdvertForCustomerID(customerId).ToList();
    foreach(AdvertView adView in adViews)
    {
        lstAdverts.Add(_advertMapper.MapToModelClass(adView));
    }
    return lstAdverts;
}

I was expecting to have some performance issues with the SQL, but the problem seems to be with the .ToList() function. I'm using ANTS performance profiler and it reports that the total runtime of the function is 1.400ms, and 850 of those is with the ToList(). So my question is, why does the tolist function take such a long time here?

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about c#4.0