Alfero Chingono
Alfero Chingono

Reputation: 2663

Entity Framework 4.0 with Sql Compact Edition 4.0 - Not Implemented Exception

I have this LINQ query:

    var children = DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)
                    .OrderBy(n => n.SortOrder);

    foreach (var child in children)
        var childNode = CreateNode(child);

When using SQL Server, everything works fine. However, when using SqlCe, I get the following error:

[SqlCeException (0x80004005): Not implemented]
   System.Data.SqlServerCe.SqlCeDataReader.ProcessResults(Int32 hr) +125
   System.Data.SqlServerCe.SqlCeDataReader.IsEndOfRowset(Int32 hr) +131
   System.Data.SqlServerCe.SqlCeDataReader.Move(DIRECTION direction) +376
   System.Data.SqlServerCe.SqlCeDataReader.Read() +95
   System.Data.Common.Internal.Materialization.Shaper`1.StoreRead() +44

[EntityCommandExecutionException: An error occurred while reading from the store provider's data reader. See the inner exception for details.]
   System.Data.Common.Internal.Materialization.Shaper`1.StoreRead() +130
   System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +46

Any idea what's going on here?

I even tried calling ToArray() before foreach, but that did not help.

EDIT:

If I change the query to:

    var children = DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)
                    .ToArray()
                    .OrderBy(n => n.SortOrder);

It works... why?

EDIT 2: BTW the Parent navigator points to the same table, so each Node can have {0..1} parent Node.

Upvotes: 4

Views: 4891

Answers (1)

Slauma
Slauma

Reputation: 177163

The fact that the query in your Edit section works indicates that the problem is in the OrderBy(n => n.SortOrder) clause, because only this part of the query ...

DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)

... is actually executed on the server. By calling .ToArray() you force the query to be executed and an (unsorted) list is loaded into memory. The following OrderBy defines a query on this list (which is an IEnumerable and not an IQueryable anymore). This second query will then be executed in memory on this list and EF or SqlCe is not involved in this sorting.

But generally SqlCe supports OrderBy, so the question remains why the first query throws an exception.

What type is Node.SortOrder you are trying to sort by? Is it nullable for instance or some "exotic" type which SqlCe is not able to sort by?

Upvotes: 1

Related Questions