I am learning Entity Framework and I am trying to get the following scenario to work.
I have a Person class and a Message class. The message class has a From property and a To property, both of type Person. I want to retrieve a list of Messages using Linq to Entities. My DB tables are Message and Person. Message has columns From and To of type int pointing to the PK ID of the Person table.
Below is the code I have so far to populate the queryable essage list. So my issue here is loading the Person data. How can I go about doing this in the most efficient way. Any explanations regarding the method would be greatly appreciated.
var messages = from m in _entities.Message
select new BizObjects.Message
{
MessageId = m.MessageId,
From = new BizObjects.Person
{
PersonId = m.From
},
To = new BizObjects.Person
{
PersonId = m.To
},
Subject = m.Subject,
Content = m.Content,
Read = m.Read,
CreatedOn = m.CreatedOn,
};
Please let me know if you need more code or background information. Thanks in advance for your help.