c# - Union Multiple Classes, Sort on Common Interface -


this question chiefly linq , possibly covariance.

two of entities implement idateditem interface. i'd union, sort these, enumerating single list. must retain entity-specific properties @ enumeration-time.

to clarify example, 1 approach tried was:

context.table1.cast<idateditem>(). union(context.table2.cast<idateditem>()). sortby(i => i.date). foreach(u => customrenderselector(u, u table1)); 

in trying various ways, i've run various errors.

  • linq entities supports casting edm primitive or enumeration types.
  • unable process type '.idateditem[]', no known mapping value layer
  • unable create constant value of type 'idateditem'. primitive types
  • etc.

the bigger picture:

  • the idateditem interface shown here simplification of actual shared properties.
  • in practice, tables filtered before union.
  • the entity-specific properties rendered, in order, in web page.
  • in parallel feature, serialized json result hierarchy.
  • i'd able perform linq aggregate operations on results well.

this requires more space comment offers. on other hand, not answer, because there no satisfying answer, really.

for union succeed, both collections must have same type (or have intrinsic conversions common types, that's covariance about).

so first go @ getting correct union be:

context.table1.select(t1 => new {                                     = t1.propa,                                     b = t1.propb,                                     date = t1.date                                 }) .union( context.table1.select(t2 => new {                                     = t2.propc,                                     b = t2.propd,                                     date = t2.date                                 })) .orderby(x => x.date) .tolist(); 

which projects both tables same anonymous type. unfortunately, because of anonymous type, can't .cast<idateditem>().

therefore, way list<idateditem> define type implements idateditem , project both tables type:

context.table1.select(t1 => new dateitem {                                     = t1.propa,                                     b = t1.propb,                                     date = t1.date                                 }) .union( context.table1.select(t2 => new dateitem {                                     = t2.propc,                                     b = t2.propd,                                     date = t2.date                                 })) .orderby(item => item.date) .asenumerable() .cast<idateditem>() 

which (i think) quite elaborate. long ef doesn't support casting interfaces in linq queries it's way go.

by way, contrary said in comment, sorting done in sql. , can use subsequent aggregate functions on result.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -