Using LINQ .Select() to cast into new type is TOO slow?

后端 未结 2 1654
长情又很酷
长情又很酷 2021-01-25 10:34

Current project, broke head over this problem:

Client Repository:

public class ClientRepository
{
    // Members
    private masterDataContext _db;

             


        
2条回答
  •  悲哀的现实
    2021-01-25 11:05

    Wow. You do a lot of looping there. Each Where inside the loop ends up looping an array to find an item, every iteration in the loop.

    Make dictionaries of the clients, so that you can look them up qickly. That should give you a dramatic increase in speed:

    public ActionResult Index()
    {
        private ClientRepository _cr = new ClientRepository();
        var _retailclients = _cr.GetRetailClientNames().ToDictionary(c => c.id);
        var _corporateclients = _cr.GetCorporateClientNames().ToDictionary(c => c.id);
        var _visits = _db.GetAllServiceVisits();
    
        var _temp = _visits.Select(o => new ServiceVisitViewModel
            {
                service_visit = o,
                client = (o.client_type ? _corporateclients[o.client_id].name : _retailclients[o.client_id].name)
            }).ToArray();
    
        return View(_temp);
    }
    

提交回复
热议问题