Few days back I put a question regarding mapping two classes Message
and MessageStatusHistory
using EF. The mapping is going fine but I am facing some
About your EDIT part: You cannot use ToList()
in a projection, just leave it an IEnumerable<T>
and convert to a List<T>
when you construct the Message
. You also don't need to create two list objects, you can switch from the LINQ to Entities query to LINQ to Objects (the second Select
) by using AsEnumerable()
:
var list = (from m in _repository.DBSet
where m.MessageIdentifier == id
select new {
// ...
Histories = m.Histories.Where(x => x.SenderId == userId)
})
.AsEnumerable() // database query is executed here
.Select(m => new Message {
// ...
Histories = m.Histories.ToList(),
// ...
}).ToList();
return list;
Be aware that Include
has no effect when you use a projection with select
. You need to make the properties that you want to include part of the projection - as you already did with select new { Histories....
.