Unable to create a constant value of type Only primitive types or enumeration types are supported in this context
c#, entity-framework, join, linq
Solution
This cannot work because `ppCombined` is a collection of objects in memory and you cannot join a set of data in the database with another set of data that is in memory. You can try instead to extract the filtered items `personProtocol` of the `ppCombined` collection in memory after you have retrieved the other properties from the database:
var persons = db.Favorites
.Where(f => f.userId == userId)
.Join(db.Person, f => f.personId, p => p.personId, (f, p) =>
new // anonymous object
{
personId = p.personId,
addressId = p.addressId,
favoriteId = f.favoriteId,
})
.AsEnumerable() // database query ends here, the rest is a query in memory
.Select(x =>
new PersonDTO
{
personId = x.personId,
addressId = x.addressId,
favoriteId = x.favoriteId,
personProtocol = ppCombined
.Where(p => p.personId == x.personId)
.Select(p => new PersonProtocol
{
personProtocolId = p.personProtocolId,
activateDt = p.activateDt,
personId = p.personId
})
.ToList()
});
Problem
I am getting this error for the query below Unable to create a constant value of type `API.Models.PersonProtocol`. Only primitive types or enumeration types are supported in this context `ppCombined` below is an `IEnumerable` object of `PersonProtocolType`, which is constructed by concat of 2 `PersonProtocol` lists. Why is this failing? Can't we use LINQ `JOIN` clause inside of `SELECT` of a `JOIN`? ``` var persons = db.Favorites .Where(x => x.userId == userId) .Join(db.Person, x => x.personId, y => y.personId, (x, y) => new PersonDTO { personId = y.personId, addressId = y.addressId, favoriteId = x.favoriteId, personProtocol = (ICollection<PersonProtocol>) ppCombined .Where(a => a.personId == x.personId) .Select( b => new PersonProtocol() { personProtocolId = b.personProtocolId, activateDt = b.activateDt, personId = b.personId }) }); ```