I have two tables having 1 to 1 relationship. One table called Person and second is PersonDetails. PesonId is in PersonDetails table as FK.
I can query individual tables like
public static Person GetPersonById(int personId)
{
using (var context = new REntities())
{
return context.Person.Where(p => p.PersonId == personId).First();
}
}
It is being used in consuming code like:
Person personInfo = PersonService.GetPersonById(personId);
and same with PersonDetail on its PK i.e. PersonDetailId
But when I have to fetch data from two tables then I am not understanding that what how would I do this and what would be the best way to return data to presentation layer.
Following would be the code to get Person and relate PersonDetails records:
from personData in context.person.Include("PersonDetail")
where personData.PersonId == personId
select personData;
What is personData here?
How can I iterate over it and get each item in client code?
Thanks.
When you get your Person object, you can include the PersonDetails object as well within the query like this…
Now on the presentation side, when you get the Person object, iterate through each item of person detail like this…
Hope it helps.