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?
 
Kyle AbrahamsConnect With a Mentor Senior .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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.