Linq join on 3 tables

Posted on 2008-10-27
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL ( and MongoDB (…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

726 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