We help IT Professionals succeed at work.
Get Started

Need help with double join in LINQ query

dgerler
dgerler asked
on
611 Views
Last Modified: 2012-06-21
Can someone help me with a double left join in LINQ?
Or perhaps suggest a better way. The goal is to get a list of the stores an appUsers has the authority to access. An appuser record's [Stores] column may contain a store number or it may contain a groupname. If a groupname, the storegroups table contains a list of stores in the group. Then I want to return the store number and the name of the store.
Give these tables:
>> Truncated for clarity <<
CREATE TABLE [dbo].[StoreGroups](
	[GroupName] [nvarchar](30) NOT NULL,
	[Store] [int] NOT NULL,
	[Client] [smallint] NOT NULL CONSTRAINT [DF_StoreGroups_Client]  DEFAULT ((1)),

CREATE TABLE [dbo].[Stores](
	[Client] [smallint] NOT NULL CONSTRAINT [DF_Stores_Client]  DEFAULT ((1)),
	[Store] [int] NOT NULL,

CREATE TABLE [dbo].[AppUsers](
	[UserID] [nvarchar](20) NOT NULL,
	[Password] [nvarchar](50) NOT NULL,
	[AuthorityLevel] [smallint] NOT NULL,
	[Stores] [varchar](30) NULL,
	[Area] [varchar](50) NULL,

Open in new window

I have this SQL query:
Select * 
from appusers au left join storegroups sg on au.stores = sg.groupname 
left join stores s on (isnumeric(au.stores) = 1 and au.stores = s.store) or sg.store = s.store

Open in new window

Here is how I tried it:
  storeString = (from au in xm.AppUsers
                           join sg in xm.StoreGroups
                           on au.Stores equals sg.GroupName into JoinedGroups
                           from jg in JoinedGroups.DefaultIfEmpty().....

Open in new window

but I got stuck with the second left join.
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE