Where to execute extra logic for linq to entities query?

Posted by Inez on Stack Overflow See other posts from Stack Overflow or by Inez
Published on 2010-03-15T21:50:00Z Indexed on 2010/03/15 21:59 UTC
Read the original article Hit count: 301

Let say that I want to populate a list of CustomerViewModel which are built based on Customer Entity Framework model with some fields (like Balance) calculated separately. Below I have code which works for lists - it is implemented in my service layer, but also I want to execute this code when I just get one item from the database and execute is as well in different services where I'm accessing Customers data as well. How should I do this to ensure performance but to to not duplicate code - the one for calculating Balance?

public List<CustomerViewModel> GetCustomerViewModelList()
{
    IQueryable<CustomerViewModel> list = from k in _customerRepository.List()
    select new CustomerViewModel
    {
        Id = k.Id,
        Name = k.Name,
        Balance =
        k.Event.Where(z => z.EventType == (int) EventTypes.Income).Sum(z => z.Amount)
    };
    return list.ToList();
}

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about asp.net-mvc