Solved

Linq join on 3 tables

Posted on 2008-10-27
3
10,273 Views
Last Modified: 2012-08-13
Hello,

I'm determined to learn linq. I'm experiemtning with a simple role managment system that we built here to play with linq.

We have a users, role, and user_roles table.

in the user table we have username and password.
in the role table we have an id (auto increment) and the role name (shipping)
in the user_roles table we have two fields ( username, and roleid) - to link the other table together.

I'm trying to write  a method like this:

public static bool AutenticateUserByRole(string uname, string pass, string role) {
        WiTracDBDataContext db = new WiTracDBDataContext();
        try {
            if (uname == "") {
                return false;
            }
            var q = (from c in db.Users
                where c.Username == uname && c.Password == pass
                join ur in db.User_Roles on c.Username equals ur.Username
                join r in db.Roles on ur.RoleID equals r.Id
                select c);
            if (q != null) {
                return true;
            }
        }
        catch { }
        return false;
    }

The problem ofcourse is i'm not actually adding the role in my where clause. Where would I put that?

thanks,

0
Comment
Question by:copyPasteGhost
  • 2
3 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22813556
Not 100% sure, but what about:

            var q = (from c in db.Users

                where c.Username == uname && c.Password == pass && r.RoleName == role

                join ur in db.User_Roles on c.Username equals ur.Username

                join r in db.Roles on ur.RoleID equals r.Id

                select c);

Open in new window

0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 0 total points
ID: 22813676
problem with that... r doesn't exist yet...since you declare it later..

I firgured out the problem.. I needed this.

var q = (from c in db.Users
                where c.Username == uname && c.Password == pass
                join dummy in (from r in db.User_Roles
                               join ur in db.Roles on r.RoleID equals ur.Id
                               where ur.Name == PageToAccess
                               select r) on c.Username equals dummy.Username
                select c);
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 22813681
thanks for your help
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now