0

Given an EntityObject, I'd like an object-oriented way find all related items as part of my data-source query.

The following produces the correct output, but brings all the rows over the wire to do it.

Parent.                                             // EntityObject
Children.                                           // EntityCollection
Where(o => o.Gender == 'm').                        // IEnumerable (local!)
OrderBy(o => o.Age).                                // IOrderedEnumerable
Skip(pages * pageSize).Take(pageSize);              // (Inefficient paging!)

I need to support a UI with this (filter using other criteria, sort, and paginate before returning results over the wire). I reordered to leverage Queryable:

Repository.                                         // Repository
Children.                                           // ObjectSet
Where(o => o.Parent == Parent && o.Gender == 'm').  // ObjectQuery, runtime error
OrderBy(o => o.Age).                                // IOrderedQueryable
Skip(pages * pageSize).Take(pageSize);

but this yields the error:

Unable to create a constant value of type 'DataModel.Parent'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

Is there a natural, object-oriented way to query on this relation, using Linq to Entities? Or do I necessarily fall-back to SQL for this?

I thought for a moment that CreateSourceQuery was going to be the answer, but it can't be applied to an EntityObject.

shannon
  • 8,664
  • 5
  • 44
  • 74

1 Answers1

1

I can't do a test for what I'm saying, but I think that you get the error because EF doesn't know how to translate o.Parent == Parent into a SQL statement. Try compare the Ids of the two parents.. o.Parent.Id == Parent.Id

Mones
  • 543
  • 1
  • 6
  • 15
  • Yeah, it was that easy. Thanks. I was getting all wound up in the confusing type transformations that were occurring through all the extension methods. Ugly stuff, that. – shannon Aug 13 '12 at 20:18
  • p.s. That was converted to a simple foreign key filter at the datasource, as you'd want it to be, rather than any redundant joins or anything. I'm surprised EF couldn't check equality on an attached entity object at the datasource. – shannon Aug 13 '12 at 20:30