473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Stuck on a LINQtoSQL Query

I have four tables:

Applications
- ApplicationID
- Name

Roles
- RoleID
- ApplicationID
- Name

Users
- UserID
- Name

UserRoles
- UserID
- RoleID

I'm trying to get applications by UserID but I can't figure out a
simple LINQ statement to do so. Can anybody help me? Basically, we
need to get the UserRoles by UserID, then get the roles from that,
then get the applications from that.

This gets the UserRoles:
var UserRoles = DataContext.Users.Single(u =u.UserID ==
UserID).UserRoles;

How do I get the apps?
var apps = DataContext.Applications.Where(a =a.Roles ...?)

Jun 27 '08 #1
2 1137
>I have four tables:
>
Applications
- ApplicationID
- Name

Roles
- RoleID
- ApplicationID
- Name

Users
- UserID
- Name

UserRoles
- UserID
- RoleID

I'm trying to get applications by UserID but I can't figure out a
simple LINQ statement to do so. Can anybody help me? Basically, we
need to get the UserRoles by UserID, then get the roles from that,
then get the applications from that.

This gets the UserRoles:
var UserRoles = DataContext.Users.Single(u =u.UserID ==
UserID).UserRoles;

How do I get the apps?
Maybe something like:
var appIDs = DataContext..Roles.Join(userRoles, r =RoleID, ur =RoleID,
(r, ur) =r.ApplicationID).Distinct();
var apps = DataContext.Applications.Where(a =>
appIDs.Contains(a.ApplicationID))

(but I'm just learning LINQ myself and there may well be a better way).

Chris Jobson
Jun 27 '08 #2
Sorry, I should have said:
var appIDs = DataContext..Roles.Join(userRoles, r =r.RoleID, ur =>
ur.RoleID, (r, ur) =r.ApplicationID).Distinct();

Chris Jobson
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gary W | last post by:
Hello, I am stuck on what is probably a very easy select query. The query is: Select Count(*), Distinct PID, ID, Name From Table 1 Group By PID I get:
1
by: Samuel Hon | last post by:
Hi I'm a bit stuck with a SELECT query. This is a simplified version of what I need. I've had a look in a few books and online but I'm definitely missing something. I'm trying to avoid looping...
2
by: Alex A via .NET 247 | last post by:
Hello all. I'm new to .NET and I am stuck and I just cannot getthis to work (even after much searching). I have a listbox populated by data from an Access Database. Themanner in which I fill the...
2
by: Bryan Field-Elliot | last post by:
We have a running server (v7.3) which, over time, seems to accumulate a bunch of "stuck" PostgreSQL instances which appear to be dead inside of a transaction. For example, "ps" reveals many of...
2
by: Mike Curry | last post by:
I have run into a problem, I have 2 fields in my database, both key fields: Table 1 ===== Field X <key> Field Y <key> In field X, there are say about 3 records for each unique Field Y. I...
1
by: colleen1980 | last post by:
When I run my query it stuck is it possible that i can debug it SELECT TOP 1 tblCredits.OPR, Min(tbl_CRCDollars.SumOfAMOUNT) AS MinOfSumOfAMOUNT, Avg(tblCredits.Credit) AS AvgOfCredit,...
1
by: Bram | last post by:
I'm writing an application using LinqToSQL. Basically the application works as follows: - Read a file - Add rows to the database (the rows belong to more than one table). - Call...
0
by: d-42 | last post by:
You wrote a great deal and there is so much I would have liked to elaborate on but this really de-railed the whole debate. You posted that linq DOES throw an exception if its not ordered, and that...
4
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I'm getting an error "Object reference not set to an instance of an object." with line number shown as <### as i try to popuate a DataGridView: private void btnList_Click(object sender, EventArgs...
1
by: =?Utf-8?B?S2ltIFM=?= | last post by:
In my professional work I work with SQL server express and VS 2008 Pro and re I know how to connect LinqtoSql. But then I work on my own computer with SQL server express and VS 2008 express its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.