A
A
Anton Polyakov2016-07-05 17:01:55
ASP.NET
Anton Polyakov, 2016-07-05 17:01:55

How to speed up a LINQ query?

There was a problem from where I did not expect ...
Here is such a monster I got with my knowledge:
data - IEnumerable T for about 400 elements.
LogicalName - byte array

var result = (from gdt in groupedDataTime
                         join ad in activeDevices
                         on gdt.Key.DeviceId equals ad.DevId
                         join a in addresses
                         on ad.GAID equals a.GAID
                         select new DeviceDto()
                         {
                             Id = (int)ad.DevId,
                             SerialNo = ad.SerialNo,
                             Address = a.UnitName,
                             DevTypeId = ad.DevTypeId,
                             SubType1 = ad.SubType1,
                             SubType2 = ad.SubType2,
                             DateInstall = ad.DateInstall,
                             GroupId = ad.GroupId,
                             Time = gdt.Key.Time
                         }).ToList();

var count = result.Count();

 for (var i = 0; i < count; i++)
            {
                result[i].Total = data.Where(x => x.DeviceId == result[i].Id && x.Time == result[i].Time && (x.LogicalName.SequenceEqual(Flags.DeviceTotal) || x.LogicalName.SequenceEqual(Flags.DeviceTotal7))).Select(x => x.NormValue).FirstOrDefault() / 1000;
            }

The catch is that the "for" loop takes an incredibly long time (400 objects in the result list is about 20-30 seconds)
Is there a way to speed up this process?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Polyakov, 2016-07-06
@Dadadumdums

changed IEnumerable to IList, everything started working instantly

T
Tsiren Naimanov, 2016-07-05
@ImmortalCAT

If possible
write Select at the beginning, this will reduce the number of columns you are pulling.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question