We have a collection of entities of type called Unit, collection of entities of type UnitProp and a collection of entities of type called Prop defined like this (simplified):
class Unit
{
int ID;
ICollection<UnitProp> UnitProps;
}
class UnitProp
{
int ID;
int UnitID;
Unit Unit;
int PropID;
Prop Prop;
}
class Prop
{
int ID;
string Description;
}
We have a List
of Prop
s (called RequiredProps) we need to query the collection with. We want to return the collection of Unit
s which satisfy the condition of having all Prop
s specified in RequiredProps.
I wrote the query like this:
var result = ctx.Units
.Where(x => RequiredProps.AsEnumerable()
.Except(x.UnitProps.Select(y => y.Prop))
.Count() == 0)
.ToList();
Of course, this is Linq to Entities so the query will throw an exception: Unable to create a constant value of type 'Prop'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
I also tried this:
var result = ctx.Units
.Where(x => RequiredProps.Select(y => y.ID)
.Except(x.UnitProps
.Select(y => y.Prop)
.Select(y => y.ID))
.Count() == 0)
.ToList();
...but it yielded the same exception.
Suggestions?