I have a LINQ to Entities query (using EF 4) that has some pretty complicated set-based filtering going on. The code compiles just fine, but when I try to run it, I get the following error:
Unable to create a constant value of type 'ITextEntity'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Now for the code. First of all, I have an interface that looks like this:
public interface ITextEntity
{
//...snip...
EntityCollection<Product> Product { get; set; }
}
I have a nested collection (actually a List<IEnumerable<ITextEntity>>
) which is a set of sets. As you can see from the ITextEntity
interface, the ITextEntity
objects in the nested sets each contain another set, a set of Product
entities. This is because they themselves are entities, with 1:N or N:N relationships back to Product
.
I have a set, Products
, that I want to compare against these nested sets in a semi-inclusive fashion. Here's the code:
List<IEnumerable<ITextEntity>> InclusiveFilters;
IQueryable<Product> Products;
//...snip...
Products = Products.Where(prod =>
InclusiveFilters.All(filter =>
filter.Any(iTextEnt =>
iTextEnt.Product.Contains(prod)
)
)
);
So what I'm trying to do is this:
- For each of several types that inherit from
ITextEntity
, you have a set S of objects of that type.- Each object O has a set O.P of products.
- For each Product prod in Products,
for each set S,
for at least one O in S,
O.P must contain prod. If not, remove prod from Products.
As you can see, this is pretty complicated.
I have the feeling that this is caused by LINQ not being able to work with the ITextEntity
type, rather than a problem with my set operations. However, the complexity of the above is making this difficult to work with, and difficult to find a non-LINQ alternative. It's going to get pretty ugly if I can't use LINQ.
I found an MSDN page and a Stack Overflow thread discussing similar exceptions, but neither was much help. Another SO thread aims a finger at my use of the Contains
method, but because of the complexity here, I haven't had much luck trying to replace it with the BuildOrExpression method. I doubt BuildOrExpression will work anyway, since this is EF 4 and Contains
is supposed to be supported.
So I'm rather stuck here. Can anyone advise?
EDIT: I'm using partials to add this interface to some of my entities, like this:
public partial class Brand : ITextEntity { }
public partial class PatternType : ITextEntity { }
public partial class Performance : ITextEntity { }
//..etc...