Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sql To Linq 2
Message
From
23/06/2010 08:10:22
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
ASP.NET
Category:
LINQ
Title:
Miscellaneous
Thread ID:
01470199
Message ID:
01470294
Views:
32
>>>Ok, I'm still leaarning Linq, so I could use some more help. The joins always throw me:
>>>
>>>
>>>SELECT r.*
>>>	FROM aspnet_Roles r
>>>	JOIN aspnet_UsersInRoles ur ON ur.RoleId = r.RoleId
>>>	JOIN aspnet_Applications a ON a.ApplicationId = r.ApplicationId
>>>	WHERE ur.UserId = @UserId AND
>>>		  r.ApplicationId = @RoleId
>>>
>>
>>Kevin,
>>Try getting your thinking away from SQL's 2D realtional model and think in object sense instead. Let's try decoding your SQL:
>>
>>1) Your final result is only from the aspnet_Roles. Others are there just for filtering.
>>2) When you build your model using the designer (L2S = DBML, L2E - EDMX) you get "navigational properties". Forget using "join" (join has a place but for the sake of understanding it forget it in this context)
>>3) Do not think SQLish. Think what you are trying to get as a human (aka it is much closer to entities/objects). IOW think simple.
>>
>>This is first draft that one to one mimics the ugly, unnecessary SQL style:
>>
>>var roles = from r in dc.aspnet_Roles
>>                join ur in aspnet_UsersInRoles on ur.RoleId equals r.RoleId
>>                join a in aspnet_Applications on a.ApplicationId equals r.ApplicationId
>>        where ur.UserId == UserId && r.ApplicationId = RoleId
>>        select r;
>>
>>
>>Since we have navigational properties in our model we don't need all those join stuff. Here is second version (since you didn't give any clue on your tables making guess):
>>
>>
>>var roles = from r in dc.aspnet_Roles
>>        where r.aspnet_UsersInRole.UserId == UserId && 
>>                 r.aspnet_Applications.ApplicationId = RoleId
>>        select r;
>>
>>
>>PS: In Linq queries plus your SQL aspnet_Applications role < g > is not easily understandable.
>>Cetin
>
>
>Ok, here's my data model: http://www.marois-consulting.com/Files/data_model.png
>
>Here's my code, which isn't compiling:
>
>
>var roles = from a in dc.aspnet_Applications
>            from r in dc.aspnet_Roles
>            from ur in dc.aspnet_UsersInRoles
>            where ur.UserID = UserID && a.ApplicationID = ApplicationID
>            select u;
>
That naturally wouldn't compile.
1) You have code errors. In C# = is assignment operator. You are trying to use it as testing. Use == instead.
2) You are trying to select "u" which is not defined (I assume you have already defined the UserID and ApplicationID parameter values). If it is defined outside this query then that would compile but would be meaningless to select a constant value N times (looks like "Select 'u' from Customers" then).

It is not clear what you are trying to select and your model is far from the SQL you originally posted. If I assume you want to select the Roles (aspnet_Roles) of a given user (aspnet_Users where UserID = something) upon a given Application ( aspnet_Applications where ApplicationId = something) then (the model is vague - I don't understand the ApplicationId member of aspnet_Roles, would assume no function):
var query = from a in mmtaspnet_ApplicationUsers
                 from ur in a.aspnet_User.aspnet_UsersInRoles
                 where a.aspnet_User.UserId == userId && a.aspnet_Application.ApplicationId == applicationId
                select ur.Aspnet_Role;
Generated SQL:
SELECT [t4].[RoleId], [t4].[ApplicationId], [t4].[RoleName]
FROM [mmtaspnet_ApplicationUser] AS [t0]
INNER JOIN [aspnet_User] AS [t1] ON [t1].[UserId] = [t0].[UserId]
CROSS JOIN [aspnet_UsersInRole] AS [t2]
INNER JOIN [aspnet_Application] AS [t3] ON [t3].[ApplicationId] = [t0].[ApplicationID]
INNER JOIN [aspnet_Role] AS [t4] ON [t4].[RoleId] = [t2].[RoleId]
WHERE ([t1].[UserId] = @p0) AND ([t3].[ApplicationId] = @p1) AND ([t2].[UserId] = [t1].[UserId])
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform