I have 2 SQL Server tables. A parent can have many children as detailed below. In this case - John smith has one child (Lisa Smith) by the relationship
People
|---------------|------------|
| PersonID | Name |
|---------------|------------|
| 1 | John Smith |
|---------------|------------|
| 2 | Lisa Smith |
|---------------|------------|
Details
|---------------|---------------|---------------|---------------|
| DetailsID | PersonID | DetailsType | ParentID |
| | | |(ref DetailsID)|
|---------------|---------------|---------------|---------------|
| 1001 |1 (John Smith) | parent | NULL |
|---------------|---------------|---------------|---------------|
| 1002 |2 (Lisa Smith) | child | 1001 |
|---------------|---------------|---------------|---------------|
In a SQL Query I would get John's children by the DetailsID like this (I already have DetailsID in the code at this point)
SELECT p.Name
FROM People p
JOIN Details d on d.PersonID = p.PersonID
WHERE ParentID = 1001
In my C# project I know how to get the list of ParentIDs in the Details table by the DetailsID but I don't know how to get the children's details. this is what I have in my mapping:
Children = (from d in dbSource.Details
where d.ParentID == 'the DetailID for parent that I have'
select d.PersonId).ToList())
This will return a list of the Childrens' IDs but I need to return a list of objects containing all children's details from the People table.
Thanks in advance.
You can use join-clasue to fetch the People data
Children = ( from p in dbSource.People
join d in dbSource.Details
on p.PersonID equals d.PersonID
where d.ParentID == 'the DetailID for parent that I have'
select p
).ToList())
You have to try join the tables using join clause try the following query
var Children = from p in dbSource.People
join d in dbSource.Details
on p.PersonId equals d.PersonId
where d.ParentId == 123 select new
{PersonId = p.PersonId, PersonName = p.Name};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.