2

Let's say I have a model created with EF 4.0

  • User

    • Roles

      • Permissions

Each entity has a DeleteDate property.

I want to get a specific user (with Name =...) and have the tree filled with items where DeletedDate == null..

This must be done with anonymous type projection as result, but I don't know how to accomplish this with a hierachy deeper than 2..

This is what I already have:

    public MyProjection MyCall(string givenName)
    {
      var result = from s in context.Users
                   where (s.Name == givenName &&
                                    s.DeletedDate == null)
                             select new
                             {
                                 s,
                                 roles = from r in s.Roles
                                         where r.DeletedDate == null
                                         select r
                             };

      var outcome = result.FirstOrDefault();

      if (outcome != null)
      {
         var myProjection = new MyProjection()
         {
             User = outcome.s,
             Roles = outcome.roles
         };

         return myProjection;
       }

    return null;
   }
Patrick Peters
  • 9,456
  • 7
  • 57
  • 106

2 Answers2

0

Depending on your structure you could do something like this:

        var result = m.Users.Where(u => u.DeletedDate == null)
            .Select( u => new
                            {   
                                u,
                                roles = u.Roles.Where(r => r.DeletedDate == null)
                                               .Select(r => new
                                                   {
                                                        r,
                                                        permissions = r.Permissions.Where(p => p.DeletedDate == null)
                                                   })
                            }).FirstOrDefault(item => item.u.Name == givenName);
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
  • How do I get one specific user, with the roles and per role the permissions then ? – Patrick Peters Oct 03 '12 at 19:11
  • I've updated the code to pick a specific user - however I don't understand what you mean by 'with the roles and per the role permissions' – Mark Oreta Oct 03 '12 at 19:22
  • The issue is more how to get a tree from user -> roles -> permissions, but then based on the deleteddate filter. I want to return this tree of one user with roles and per role the permission back to the UI... – Patrick Peters Oct 03 '12 at 19:30
0

If you retrieve with the following:

 var result = from s in MyUsers
              where s.DeletedDate == null
              select new aUser{
                   Roles = (from r in s.Roles
                            where r.DeletedDate == null
                            select r).ToList()
              };

And then create a TreeView:

 TreeView treeView = new TreeView();

Then set the ItemsSource of the TreeView to the IEnumerable:

 treeView.ItemsSource = result;

Then build a HierarchicalDataTemplate in your TreeView to represent your Lists (similar to this or for more in depth this), then voila!

Community
  • 1
  • 1
Bob.
  • 3,894
  • 4
  • 44
  • 76