Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am struggling to get certain information from my database by using LINQ.

The initial database structure that is required for the system looks as follows:

Please see for image: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/1f665aa3-ac69-4e81-91ae-5c9ee0bfe6b1[^]

What I need to get is to retrieve the Project name or specific project detail per OD. This however depends on the user that logs on to the system. As per the diagram above, users will be part of Sponsers, Owners, OD Leaders or Risk Managers.

In other words, if the user that logs on is a Risk Manager for a certain OD, I need to use his username and retrieve only the project details and specific projects that his user is mapped to.

How the system currently works: When a new project is added, existing or new project sponsers and owners are assigned to the project. If new delegates are added, they are automatically added to the user authentication and assigned the sponser or owner “roles” respectively. Similarly, when respective OD details are then added to this project, new or existing OD leaders and risk managers are assigned to the detail, and again the user authentication and rights are assigned etc…

Now, for example when a risk manager opens the system, I know what role his is part of. But because he still needs to drill through from top level, being the Project, I need to look at the tblRiskManagers table, find the related project OD detail records he is allowed to see, and then get to the top to know that he is only allowed to see project x which he is part of.

What a user can see is dependent on the relationships between the tables…

PLEASE can someone assist as matter of urgency?

I have searched for answers and got something down, but cant get anything to work. Will paste existing code in couple of hours when at work. Currently its 2AM in morning and my brain is bit fried...

(Oh, I have been using LightSwitch to build my user interface and linking to the SQL 2008 R2 database.)

C#
Please note: This has been done on the "All_PreprocessQuery" in the dataservice class of Lightswitch for my tblProjects object...


            if (this.Application.User.IsInRole(Permissions.ManageAdmin))
            {
                // use full query, no limitation
            }
            else 
            if (this.Application.User.HasPermission(Permissions.ProjectOwners))
            {   
                query = from q in query
                    .Where(x => x.tblProjOwnerMaps == x.tblProjOwnerMaps
                        .Where(y => y.tblProcessOwner.UserName == this.Application.User.Name))
                        select q;

            }
            else if (this.Application.User.HasPermission(Permissions.ProjectSponsers))
            {
                query = from q in query
                    .Where(x => x.tblProjSponserMaps == x.tblProjSponserMaps
                        .Where(y => y.tblProjectSponser.UserName == this.Application.User.Name))
                        select q;

            }
            else if (this.Application.User.HasPermission(Permissions.OperatingDivisionLeader))
            {          
                query = from q in query
                            .Where(x => x.ProjectDetail == this.ProjectDetails
                                .Where(y => y.OperatingDivision.ODLeaderODMap == this.OperatingDivisions
                                    .Where(z => z.ODLeaderODMap == this.ODLeaderODMaps
                                        .Where(a => a.ODLeader.UserName == this.Application.User.Name)).AsQueryable()))
                        select q;


            }
            else if (this.Application.User.HasPermission(Permissions.ProjectODManager))
            {
                query = from q in query
                            .Where(x => x.ProjectDetail == x.ProjectDetail
                                .Where(y => y.ProjRiskManMap == y.ProjRiskManMap
                                    .Where(a => a.ODManager.UserName == this.Application.User.Name)))
                        select q;
                

            }
Posted
Updated 13-Sep-11 20:59pm
v2
Comments
BobJanova 14-Sep-11 6:42am    
Good question. What makes this hard is the many-to-many relationship of e.g. Risk Managers to Projects. You might have to make a filter query of 'all projects this person can see' (easy enough, e.g. select RiskManager.riskmanager, projectid from RiskManager left join ProjectRiskManagerMap on RiskManager.id=ProjectRiskManagerMap.riskmanager where riskmanager = <username>), and then use that list of projects in a 'where ... in' clause.

I'm not sure how to do that in LINQ though.
BillWoodruff 15-Sep-11 4:30am    
Bob, even your comments are always so helpful, I find I wish I could upvote them ! :) best, Bill

1 solution

After some scope creep and other changes that had to be implemented, I also sorted out this issue.

For a detailed view of the code as well as the NEW DB structure according to business needs, please see the following link:

http://social.msdn.microsoft.com/Forums/en-ZA/linqprojectgeneral/thread/1f665aa3-ac69-4e81-91ae-5c9ee0bfe6b1[^]

(Scroll to the "Answers" part I inserted....)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900