c# - What is the fastest way to compare a value from a list to a specific sum from another list? -


i have 2 huge lists of created objects. list<forecast> forecasts different resources , list<capacity> capacities of these resources.

a forecast contains booleans indicating if resource on or below capacity sum of forecasts.

public class forecast {     public int resourceid { get; set; }     public double? forecastjan { get; set; }     // , forecastfeb, forecastmarch, forecastapr, forecastmay, etc.      public bool isoverforecastedjan { get; set; }     // , isoverforecastedfeb, isoverforecastedmarch, isoverforecastedapr, etc. }  public class capacity {     public int resourceid { get; set; }     public double? capacityjan { get; set; }     // , capacityfeb, capacitymar, capacityapr, capacitymay, etc. } 

i have set isoverforecastxxx properties have know each month if sum of forecasts each resource higher sum of capacity specific resource.

here code :

foreach (forecast f in forecastlist) {     if (capacitylist.where(c => c.id == f.resourceid)                     .select(c => c.capacityjan)                     .first()         < forecastlist.where(x => x.resourceid == f.resourceid)                       .sum(x => x.forecastjan)     )     {         f.isoverforecastedjan = true;     }      //same each month... } 

my code works have bad performances when lists big (thousands of elements).

do how of can improve algorithm ? how compare sum of forecasts each resource associated capacity ?

you can use first or firstordefault capacities currect resource, compare them. use tolookup similar dictionary forecasts resources.

ilookup<int, forecast> forecastmonthgroups = forecastlist     .tolookup(fc => fc.resourceid); foreach (forecast f in forecastlist) {     double? jansum = forecastmonthgroups[f.resourceid].sum(fc => fc.forecastjan);     double? febsum = forecastmonthgroups[f.resourceid].sum(fc => fc.forecastfeb);     var capacities = capacitylist.first(c => c.resourceid == f.resourceid);     bool overjan = capacities.capacityjan < jansum;     bool overfeb = capacities.capacityfeb < febsum;     // ...     f.isoverforecastedjan = overjan;     f.isoverforecastedfeb = overfeb;     // ... } 

it seems there 1 capacity per resourceid, use dictionary store "way" resourceid capacity, improve performance more:

ilookup<int, forecast> forecastmonthgroups = forecastlist     .tolookup(fc => fc.resourceid); dictionary<int, capacity> capacityresources = capacitylist     .todictionary(c => c.resourceid); foreach (forecast f in forecastlist) {     double? jansum = forecastmonthgroups[f.resourceid].sum(fc => fc.forecastjan);     double? febsum = forecastmonthgroups[f.resourceid].sum(fc => fc.forecastfeb);     bool overjan = capacityresources[f.resourceid].capacityjan < jansum;     bool overfeb = capacityresources[f.resourceid].capacityfeb < febsum;     // ...     f.isoverforecastedjan = overjan;     f.isoverforecastedfeb = overfeb;     // ... } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -