Finding if value exists in list with LINQ and saving info into a dataset, possibly -


i brand new linq, have loops through orgs user belongs to, make sure have permissions various operations on form.

looks this:

//loop through user orgs see if selected, have access foreach (orgpermission userorg in user.orgs)             {                 //get org permissions selected org                 if ((ddlorg.selectedvalue == (userorg.org.orgcode + "-" + userorg.org.orgsubcode)))                 { if (userorg.type.contains("3") || userorg.type.contains("00"))                         { /  /do here. }}} 

i trying rid of loop. if user has lots of orgs it's taking little while run, , i'm trying optimize application run time.

i tried following:

  bool has = user.orgs.any(cus => cus.org.orgcode + "-" + cus.org.orgsubcode == ddlorg.selectedvalue); 

as can see, ddlorg dropdown value in org-suborg format. i'm getting false.

i save result, not in bool, possibly single user.org found, can use check permissions , other stuff.

am on right track here? please point me in right direction.

you typically wouldn't rid of loop, rather filter items being looped:

var users = user.orgs.where(userorg => ddlorg.selectedvalue == (userorg.org.orgcode + "-" + userorg.org.orgsubcode) && (userorg.type.contains("3") || userorg.type.contains("00")));   foreach(orgpermission userorg in users)  {       // operation  } 

that being said, not going faster, linq query still looping through items, potentially simpler maintain.

if need single orgpermission (ie: first), speed via:

var userorg = user.orgs.firstordefault(userorg => ddlorg.selectedvalue == (userorg.org.orgcode + "-" + userorg.org.orgsubcode) && (userorg.type.contains("3") || userorg.type.contains("00")));  // userorg orgpermission if found, or null if not. 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -