Linq join on 3 tables

Posted on 2008-10-27
Medium Priority
Last Modified: 2012-08-13

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?


Question by:copyPasteGhost
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
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

LVL 13

Accepted Solution

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);
LVL 13

Author Comment

ID: 22813681
thanks for your help

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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