Linq/Lambda left join

Hello... new to linq lambda - what is wrong with my query? - I get 'sequence contains no elements' error when there are no providerSpecialties records for the given provider.

This should behave like a left join from the provider entity.

            
             return DBContext.Provider.GroupJoin(DBContext.ProviderSpecialties,
                    p => p.providerId, ps => ps.providerSpecialtyId, (p, ps) => new { Provider = p, ProviderSpecialties = ps.DefaultIfEmpty()})
                    .SelectMany(a => a.ProviderSpecialties
                    .Select(b => new { Provider = a.Provider, ProviderSpecialties = b }))
                    .Where(w => w.ProviderSpecialties.isPrimary == true)
                    .Where (w => w.Provider.providerId == providerId)
                    .Select(m => new ProviderDetail
                    {
                        providerId = m.Provider.providerId,
                        NPI = m.Provider.NPI,
                        licenseNumber = m.Provider.licenseNumber,
                        licenseExpiration = m.Provider.licenseExpiration,
                        licenseStateId = m.Provider.licenseState,
                        licenseStateName = m.Provider.State.abbreviation,
                        networkLevelId = m.Provider.networkLevel,
                        networkLevelName = m.Provider.NetworkLevel1.name,
                        firstName = m.Provider.firstName,
                        lastName = m.Provider.lastName,
                        title = m.Provider.title,
                        birthDate = m.Provider.birthDate,
                        sex = m.Provider.sex,
                        primarySpecialtyId = m.ProviderSpecialties.Specialty.specialtyId,
                        referrable = m.Provider.referrable,
                        photoUrl = m.Provider.photoUrl,
                        emailAddress = m.Provider.emailAddress,
                        websiteUrl = m.Provider.websiteUrl,
                        recordStatus = m.Provider.recordStatus
                    }).Single();

Open in new window


In SQL I would write:

select p.*, ps.specialtyId, ps.specialtyName
from provider p
left join providerspecialties ps
on ps.providerId = p.providerId
and ps.isPrimary = 1
left join Specialty s
on ps.specialtyId = s.specialtyId

Open in new window


Thanks
sailing_12Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sailing_12Author Commented:
Was able to solve this-

return DBContext.Provider
		.GroupJoin(PMTDBContext.ProviderSpecialties, p => p.providerId, ps => ps.providerId, (x, y) => new { Provider = x, ProviderSpecialties = y })
		.Where(w => w.Provider.providerId == providerId)
		.SelectMany(x => x.ProviderSpecialties.Where(w => w.isPrimary == true)
		.DefaultIfEmpty(), (x, y) => new { Provider = x.Provider, ProviderSpecialties = y })
		.Select(m => new ProviderDetail
		{
			providerId = m.Provider.providerId,
			NPI = m.Provider.NPI,
			licenseNumber = m.Provider.licenseNumber,
			licenseExpiration = m.Provider.licenseExpiration,
			licenseStateId = m.Provider.licenseState,
			licenseStateName = m.Provider.State.abbreviation,
			networkLevelId = m.Provider.networkLevel,
			networkLevelName = m.Provider.NetworkLevel1.name,
			firstName = m.Provider.firstName,
			lastName = m.Provider.lastName,
			title = m.Provider.title,
			birthDate = m.Provider.birthDate,
			sex = m.Provider.sex,
			primarySpecialtyName = m.ProviderSpecialties.Specialty.epicName,
			primarySpecialtyId = m.ProviderSpecialties.specialtyId == null ? 0 : m.ProviderSpecialties.specialtyId,
			referrable = m.Provider.referrable,
			photoUrl = m.Provider.photoUrl,
			emailAddress = m.Provider.emailAddress,
			websiteUrl = m.Provider.websiteUrl,
			recordStatus = m.Provider.recordStatus
		}).Single();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sailing_12Author Commented:
Needed to add the where isprimary = true to the .selectmany instead of on the main query.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.