Question: This question is chiefly about LINQ and possibly covariance.

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).
ForEach(u => CustomRenderSelector(u, u is Table1));

```

In trying to do this various ways, 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
* Unable to create a constant value of type 'IDatedItem'. Only primitive types
* etc.

The bigger picture:

* The IDatedItem interface shown here is a simplification of the actual shared properties.
* In practice, the tables are filtered before the union.
* 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.
* I'd like to be able to perform LINQ aggregate operations on the results as well.

Answer: 
This requires more space than a comment offers. On the other hand, this is not really an answer, because there is no satisfying answer, really.

For a `Union` to succeed, both collections must have the same type (or have intrinsic conversions to common types, that's what covariance is about).

So a first go at getting a correct Union could be:

```
Context.Table1.Select(t1 => new {
                                    A = t1.PropA,
                                    B = t1.PropB,
                                    Date = t1.Date
                                })
.Union(
Context.Table1.Select(t2 => new {
                                    A = t2.PropC,
                                    B = t2.PropD,
                                    Date = t2.Date
                                }))
.OrderBy(x => x.Date)
.ToList();

```

which projects both tables to the same anonymous type. Unfortunately, because of the anonymous type, you can't do `.Cast<IDatedItem>()`.

Therefore, the only way to get a `List<IDatedItem>` is to define a type that implements `IDatedItem` and project both tables to that type:

```
Context.Table1.Select(t1 => new DateItem {
                                    A = t1.PropA,
                                    B = t1.PropB,
                                    Date = t1.Date
                                })
.Union(
Context.Table1.Select(t2 => new DateItem {
                                    A = t2.PropC,
                                    B = t2.PropD,
                                    Date = t2.Date
                                }))
.OrderBy(item => item.Date)
.AsEnumerable()
.Cast<IDatedItem>()

```

Which (I think) is quite elaborate. But as long as EF doesn't support casting to interfaces in linq queries it's the way to go.

By the way, contrary to what I said in my comment, the sorting will be done in SQL. And you can use subsequent aggregate functions on the result.
Here's the working code. My solution was to ensure all data was local, to keep LINQ-to-EF from trying to do all manner of things it knows it can't that were causing many unclear errors. Then a simple type declaration on the generic Union can take hold.

That means that, aside from the annoyances of LINQ-to-EF, the main issue here is really a duplicate of [LINQ Union different types - dynamically casting to an interface?](https://stackoverflow.com/questions/13540846/linq-union-different-types-dynamically-casting-to-an-interface) .

```
public virtual ActionResult Index() {
    return View(StatusBoard().OrderBy(s => s.Status));
}

private IEnumerable<DefensiveSituationBoardMember> StatusBoard() {
    DateTime now = DateTime.UtcNow;
    DateTime historicalCutoff = now.AddDays(-1);
    IEnumerable<Member> activeMembers = Context.Members.Where(n => !n.Disabled).ToList();

    // IncomingAttack and Reinforcements both implement IDefensiveActivity
    IEnumerable<IncomingAttack> harm = Context.IncomingAttacks.Where(n => n.IsOngoingThreat || n.ArrivalOn > historicalCutoff).ToList();
    IEnumerable<Reinforcements> help = Context.Reinforcements.Where(n => !n.Returned.HasValue || n.Returned > historicalCutoff).ToList();

    // Here's the relevant fix
    IEnumerable<IDefensiveActivity> visibleActivity = help.Union<IDefensiveActivity>(harm);

    return from member in activeMembers
        join harmEntry in harm on member equals harmEntry.DestinationMember into harmGroup
        join activityEntry in visibleActivity on member equals activityEntry.DestinationMember into activityGroup
        select new DefensiveSituationBoardMember {
            Member = member,
            Posture = harmGroup.Max(i => (DefensivePostures?)i.Posture),
            Activity = activityGroup.OrderBy(a => a.ArrivalOn)
        };

}

```