Nested Tree Like Object Queries - How?

  • Thread starter Thread starter wildThought
  • Start date Start date
W

wildThought

Given the following classes:




public class aUser

{

public string name;

public UserRole[] userRoles;

}


public class userRole

{

public string roleName;

public ApplicationRole[] applicationRoles;

}

public class ApplicationRole

{

public AProcess[] theProcesses;

}



public class AProcess

{

public string processName;

}



I can



string processName =
aUser.userRoles[0].applicationRoles[0].theProcesses[0].processName



How can I find out if a user has a processName that matches a given
string anywhere nested in the userRoles > ApplicationRoles >
TheProcessesCollection > ProcessInstance



If I was going to do this without LINQ or Lambdas



foreach(userRole role in user.userRoles)

{

foreach(ApplicationRole appRole in role.applicationRoles)

{

foreach(AProcess proc in appRole.theProcesses)

{

if(proc.processName == 'SomeProcessString')

{

return true;

}

}

}

}





IN SQL we could join a bunch of tables together and select a count(*)
into a variable to accomplish the same task.



Select count(*)
FROM User u
JOIN UserRole ur ON (ur.UserID = u.UserID)
JOIN ApplicationRole ar ON (ar.RoleID = ur.RoleID)
JOIN RoleProcesses rp ON (rp.RoleID = ar.RoleID)
JOIN Process p ON (p.ProcessID = rp.ProcessID)
WHERE p.ProcessName = 'Some Process Name'




How do I solve the above using LINQtoSQL?

How do I solve the above using Lamda?
 
Having spent several years working with complex unrelated hierarchies
like this, I can tell you it is far easier to manage each of these object
types
as entirely separate generic List<T>. Then, writing Find predicate queries
against
each list that deal with all sorts of different scenarios is much, much
easier to support
as your app becomes more and more complex.

I realize the methodology you've used is considered "standard". That said,
the way people did things years ago doesn't necessarily mean it is the best
fit for "your" app.

--
Robbe Morris [Microsoft MVP - Visual C#]
AdvancedXL Server, Designer, and Data Analyzer
Convert cell ranges in Excel to rule driven web apps
without IT programmers.
Free download: http://www.equalssolved.com/default.aspx
 
Back
Top