I try to join a navigation property by using LINQ. This is my code
var activities = Using<GetActivitiesForUser>()
.Execute(User.DisplayAs, User.MarketId);
var contacts = Using<GetContactsForUser>()
.Execute(User.DisplayAs, User.MarketId);
var model = from activity in activities
join contact in contacts
on activity.ContactId equals contact.ContactId
select activity;
This code will only select the activity without any join. The activity has a property named Contact, and i want to join that property from Contacts.
UPDATE
Example to solve it
foreach(var activity in activities)
{
activity = from contact in contacts
where contact.ContactId=activity.ContactId
select contact
}
But i want to do this with a join.
You must include both activities and contacts in the query result:
from activity in activities
join contact in contacts
on activity.ContactId equals contact.ContactId
select new { activity, contact };
or select individual properties of both objects.
Edit After your comment:
You won't be able to let Dapper populate activity.contact, it is deliberately kept simple. What you can do is loop through the results and set the property yourself:
foreach(var anon in model)
{
anon.activity.contact = anon.contact;
}
where model is the query .ToList()
.
Another effective, but ugly way could be:
model.Select(x => { x.activity.contact = x.contact; return activity; }).ToList();
Which immediately returns the modified activities. It is ugly because it abuses a method that is intended to be stateless (Select
), but yeah...
It looks like you are using the Entity Framework
try
var model = activities.Include("Contact");