Allan
asked on
Need Help With Simple LINQ Syntax
Hi Experts!
Need your help updating LINQ query.
The LINQ query should return a distinct record of locationId and phonenumber.
Then which need to extract locationId and phonenumber and store it in local variables.
SQL Query:
C# method:
TIA!
Need your help updating LINQ query.
The LINQ query should return a distinct record of locationId and phonenumber.
Then which need to extract locationId and phonenumber and store it in local variables.
SQL Query:
SELECT DISTINCT
personlocation.LocationID,
phone.PhoneNumber
FROM dbo.personProducts pp
INNER JOIN dbo.personPhonesLocations phoneLocation ON pp.personLocationRecID = phoneLocation.personLocationRecID
INNER JOIN dbo.personLocations personlocation ON pp.personLocationRecID = personlocation.personLocationRecID
INNER JOIN dbo.Phones phone ON phoneLocation.PhoneID = phone.PhoneID
INNER JOIN dbo.PhoneTypes PhoneTypes ON pl.PhoneTypeID = PhoneTypes.PhoneTypeID
WHERE pp.PersonNumber = 'ABC123'
AND PhoneTypes.PhoneTypeName = 'Contact Number'
C# method:
var personPhone = (from pp in dataContext.personProducts
from phoneLocation in dataContext.personPhonesLocations
from personLocation in dataContext.personLocations
from phone in dataContext.Phones
from phoneType in dataContext.PhoneTypes
where pp.PersonNumber == "ABC123" && phoneType.PhoneTypeName == "Contact Number"
select new
{
LocationId = personLocation.LocationID,
phonenumber = phone.PhoneNumber
}).Distinct();
if (personPhone != null)
{
string locationId = ???; <-- need help
string phonenumber = ?? <-- need help
}
TIA!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks HainKurt,
I was missing FirstOrDefault() after the distinct.
or could've iterate through the collection like you said. Thanks!
I was missing FirstOrDefault() after the distinct.
or could've iterate through the collection like you said. Thanks!
Yes you can use ToList() or FirstorDefault() according to your requirement.
Open in new window