Friday, 23 August 2013

LINQ Sort on Interface Property

LINQ Sort on Interface Property

Two of my Entities implement the IDatedItem interface. I'd like to union,
then sort these, for enumerating as a single list. I must retain
entity-specific properties at enumeration-time.
To clarify by example, one approach I tried was:
Context.Table1.Cast<IDatedItem>().
Union(Context.Table2.Cast<IDatedItem>()).
SortBy(i => i.Date).
ToList();
In trying to do this, I've run into various errors.
LINQ to Entities only supports casting EDM primitive or enumeration types.
Unable to process the type '.IDatedItem[]', no known mapping to the value
layer
etc.
Details:
The entity-specific properties will be rendered in order in a web page.
In a parallel feature, they will be serialized to a JSON result hierarchy.
The IDatedItem interface here is a simplification of the actual shared
properties.
In practice, the tables are filtered before the union.
I'd like to be able to perform LINQ aggregate operations on the results as
well.

No comments:

Post a Comment