Join table to lambda expression

How do I join master locations table to the below lambda expression?

In SQL I would write:

SELECT * 
FROM ProviderLocations p
JOIN Location l
ON p.locationId = l.locationId
WHERE p.providerId = @providerId
AND p.isActive = true

Open in new window



            return PMTDBContext.ProviderLocations
               .Where(w => (w.providerId == providerId && w.isActive == true)).ToList();

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.

Kyle AbrahamsSenior .Net DeveloperCommented:
using http://www.tutorialsteacher.com/linq/linq-joining-operator-join as a guide:

private int theProviderId = 2;  // pass in as variable

return PMTDBContext.ProviderLocations.Join(PMTDBContext.Location, provLocation => provLocation.locationId, loc => loc.locationId, (provLocation, loc) => new 
{ 
  provLocation, 
  loc
}).Where(o => o.provLocation.ProviderId == theProviderId  && o.provLocation.isActive== 1).ToList();

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:
OK, but I get the following error:

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: PMT.Core.Model.ProviderLocations provLocation, PMT.Core.Model.Location loc>>' to 'System.Collections.Generic.IList<PMT.Core.Model.ProviderLocations>'. An explicit conversion exists (are you missing a cast?)
0
Kyle AbrahamsSenior .Net DeveloperCommented:
If you're only looking for Provider locations just change the new portion:
(provLocation, loc) => new 
{ 
  provLocation
})

Open in new window

0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

sailing_12Author Commented:
I'm not following that, no I'm not looking for just provider locations - the reason for joining the locations table is to pull in needed attributes on the master address records.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
can you post your function as is ?
0
sailing_12Author Commented:
      
  public IList<ProviderLocations> GetProviderLocations(int providerId)
        {
            return PMTDBContext.ProviderLocations
               .Where(w => (w.providerId == providerId && w.isActive == true)).ToList();             
        }

Open in new window

0
Kyle AbrahamsSenior .Net DeveloperCommented:
public IList<ProviderLocations> GetProviderLocations(int providerId)

You're returning a list of providerlocations . . . which means you'll only ever have those fields available outside the function.

If you're looking to return locations you would need to change your signature to
IList<Locations> getLocations(providerId)

Open in new window



if you're looking for both then I would go with a tuple
public Tuple<List<ProviderLocations>, List<Locations>> getCombined(int ProviderId)

Open in new window


But then you'll need to process the result set to populate the tuple.
0
aditya kinCommented:

you can study
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
Microsoft SQL Server

From novice to tech pro — start learning today.