c# - Most efficient (& fastest) way to query a list -


i'm trying work out performant way query list. know there ton of examples out there , has come loads before, i'm new , i'm struggling how apply of concepts situation.

private static void keepmatchesbasedonrestrictions(ref list<entity> matches,          list<entity> prefilteredshifts, list<entity> locationalinformations)     {         if (matches.count == 0) return;          matches.removeall(              (match) => ( geographyhasrestriction(match, prefilteredshifts, locationalinformations) )              );     }  private static bool geographyhasrestriction(entity match, list<entity> prefilteredshifts, list<entity> locationalinformations)     {                           entityreference fw = match.getattributevalue<entityreference>("crm_fw");          entity shift = prefilteredshifts.single<entity>(                  => match.getattributevalue<entityreference>("crm_shift").id == a.id             );         entityreference trust = shift.getattributevalue<entityreference>("crm_trust");         entityreference location = shift.getattributevalue<entityreference>("crm_location");         entityreference ward = shift.getattributevalue<entityreference>("crm_ward");          dictionary<guid, entity> locinforecs = locationalinformations.todictionary(p => p.id);          var locationalinformationquery = loc in locationalinformations                                          (                                             (                                                 loc.getattributevalue<entityreference>("crm_fw").id == fw.id                                                 && !loc.contains("crm_trust")                                                 && !loc.contains("crm_location")                                                 && !loc.contains("crm_ward")                                             )                                             ||                                             (                                                 loc.getattributevalue<entityreference>("crm_fw").id == fw.id                                                 && loc.getattributevalue<entityreference>("crm_trust").id == trust.id                                                 && !loc.contains("crm_location")                                                 && !loc.contains("crm_ward")                                             )                                             ||                                             (                                                 loc.getattributevalue<entityreference>("crm_fw").id == fw.id                                                 && loc.getattributevalue<entityreference>("crm_trust").id == trust.id                                                 && loc.getattributevalue<entityreference>("crm_location").id == location.id                                                 && !loc.contains("crm_ward")                                             )                                             ||                                             (                                                 loc.getattributevalue<entityreference>("crm_fw").id == fw.id                                                 && loc.getattributevalue<entityreference>("crm_trust").id == trust.id                                                 && loc.getattributevalue<entityreference>("crm_location").id == location.id                                                 && loc.getattributevalue<entityreference>("crm_ward").id == ward.id                                             )                                          )                                          select loc;          foreach (entity loc in locationalinformationquery)         {             if (loc.getattributevalue<bool>("crm_hasrestriction"))             {                 return true;             }         }          //return false;     } 

so think problem 2-fold;

  1. the locationalinformationquery query seems run slowly... i'm talking in region of 2 seconds per iteration horrible.
  2. i suspect approach of calling matches.removeall() flawed due performance issues regarding lists.

so in terms of addressing this, think may able better performance converting locationalinformations list other type of container such dictionary, hashset or sortedlist. problem have no idea how go adjusting query to take advantage of more efficient containers.

as far second point goes, i'd curious hear alternatives using list.removeall(). have flexibility modify incoming container types within reason may viable.

with regards list sizes in case of use, match contains few thousand items , prefilteredshifts , locationalinformations each contain > 100,000 items.

as aside i've tried using parallel.foreach instead of foreach, made virtually no difference whatsoever.

edit: clarify questions, i'm doing in memory. i've populated of lists there shouldn't additional round trips db. i'm reasonably getattributevalue<entityreference> doesn't initial further db overhead.

also, yes local application calling dynamics crm online.

the code -

foreach (entity loc in locationalinformationquery)     {         if (loc.getattributevalue<bool>("crm_hasrestriction"))         {             return true;         }     } 

can 1 reason slowness. fetching more data , enumerating them in memory. can perform check directly before fetch, fetch lesser data , faster. -

return (from loc in locationalinformations                                      ((                                         (                                             loc.getattributevalue<entityreference>("crm_fw").id == fw.id                                             && !loc.contains("crm_trust")                                             && !loc.contains("crm_location")                                             && !loc.contains("crm_ward")                                         )                                         ||                                         (                                             loc.getattributevalue<entityreference>("crm_fw").id == fw.id                                             && loc.getattributevalue<entityreference>("crm_trust").id == trust.id                                             && !loc.contains("crm_location")                                             && !loc.contains("crm_ward")                                         )                                         ||                                         (                                             loc.getattributevalue<entityreference>("crm_fw").id == fw.id                                             && loc.getattributevalue<entityreference>("crm_trust").id == trust.id                                             && loc.getattributevalue<entityreference>("crm_location").id == location.id                                             && !loc.contains("crm_ward")                                         )                                         ||                                         (                                             loc.getattributevalue<entityreference>("crm_fw").id == fw.id                                             && loc.getattributevalue<entityreference>("crm_trust").id == trust.id                                             && loc.getattributevalue<entityreference>("crm_location").id == location.id                                             && loc.getattributevalue<entityreference>("crm_ward").id == ward.id                                         )                                      ) && loc.getattributevalue<bool>("crm_hasrestriction")) // check before fetch in here                                      select loc).any();  

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -