views:

445

answers:

1

I have a Pages table, I have a PagesRoles table with PageId, RoleID that links to ASP.Net Membership Roles table and the Pages table.

I want to somehow return a Page that links to the currently logged in User's Roles.

The User may be in more than one role. A Page can have more than one Role against it.

I have a Page class that has a foreign key property of IQueryable. PageRoles has a IQueryable to ASPNet Roles table (IQueryable) and IQueryable

Thanks

EDIT:

Using Adam's answer to get all Pages that have a role assigned to it which matches one of the currently logged in user's roles I have a added issue. I have a Menu table that has a PageID foreign key and a CategoryID.

I would like to return all Menu items with a CategoryID of 4. For those that have a PageID I need to make sure that the user is allowed to see that Page based on his role. I think Adam's query would do that. So I think what I need is a UNION, return all Menu items where PageID is null and CategoryID is 4 and return all Menu items where CategoryID is 4 and join to the Page->PageRole->ASPNetRole where current users roles is in that ASPNetRole results.

A: 

If you're using SubSonic 3 a query like the following should work:

var pagesForUser = from pages in Pages.All()
    join pagesRoles in PagesRoles.All() on pages.Id equals pagesRoles.PageID
    join roles in Roles.All() on pagesRoles.RoleId equals roles.Id
    where User.GetRoles().Contains(roles.Name)
  select pages;
Adam
Thats brill, thanks. I am really struggling with the LINQ stuff and the correct syntax to use for Subsonic but thats a different problem.I have one added issue that I should have mentioned. I have a menu table that has a PageID column. Not all menu items have a PageID but for those that do I need to do your query but I would like to combine it so I can select all from menu where pageid is null or your query. I have outlined the issue here - http://stackoverflow.com/questions/1038309/reengineer-sql-into-subsonic-linq if it helps. Hopefully you see what I mean. Thanks again
Jon
No problem, I'll have a look at the other question. If you want to get up to speed on linq I'd suggest getting a copy of the o'reilly pocket reference, it's a great little book.
Adam
Thanks. Hopefully you understand what I am trying to do. I have changed my Model since the question was asked and replace the Roles column with a Many to Many PagesRoles table. Might get that book, its just that I'm confused with doing things like joins in C# but you have a foregin key property in the subsonic class so I thought I was supposed to use that.
Jon
I tried this with no luck.var test = (from menu in Menu.All().Where(x => x.PageID == null) select menu).Union( from menu2 in Menu.All() join pages in WebPage.All() on menu2.PageID equals pages.ID join pagesRoles in PageRole.All() on pages.ID equals pagesRoles.PageID join roles in aspnet_Role.All() on pagesRoles.RoleId equals roles.RoleId where Roles.GetRolesForUser().Contains(roles.RoleName) select menu2);
Jon
Can you edit your other question or create a new one for this, that should make it easier.
Adam
Added a new question! Tagged Subsonic/Subsonic 3
Jon