Search code examples
c#asp.netlinqlinq-to-sqlnotsupportedexception

LINQ: "Types in Union or Concat are constructed incompatibly."


I'm relatively new to ASP.NET and LINQ but I encountered a very strange behaviour that is nearly driving me crazy. I found a few discussions about the error mentioned in the title, but none seemed to fit my problem. Please have a look at the following snapshot from the Object Relational Designer:

Screenshot from ORD, showing the data model

As you can see, a Relation stands for the connection of two Anchors. So for this there are two foreign keys defined for table Relation: Relation.AnchorIDFrom => Anchor.AnchorID and Relation.AnchorIDTo => Anchor.AnchorID. None of those columns is nullable. My goal now is, given a list of IDs of anchors, to retrieve all Relations any of these anchors participates in. Here's the method for this:

[WebMethod]
public string GetRelations(String token, List<Guid> anchorIDs)
{
    JavaScriptSerializer js = new JavaScriptSerializer();
    LinkItCoreDataContext dc = new LinkItCoreDataContext();

    IQueryable relationQry =
        from anchor in dc.Anchors
        where anchorIDs.Contains(anchor.AnchorID)
        //stupid names because object relational designer
        //does not seem to support custom names:
        from relation in anchor.Relations.Union(anchor.Relations1)
        select new
        {
            relationID = relation.RelationID,
            anchorIDFrom = relation.AnchorIDFrom,
            anchorIDTo = relation.AnchorIDTo,
            relationTypes = from type in relation.RelationTypes
                            //stupid names again:
                            select type.RelationType1
        };
    return js.Serialize(relationQry);//Here's where the error occurs
}

At runtime this gives me a NotSupportedException stating Types in Union or Concat are constructed incompatibly. I think this is quite weird, because the expression anchor.Relations.Union(anchor.Relations1) should merge two things that are fully equal in a syntactical sense. If I change the critical line to from relation in anchor.Relations or from relation in anchor.Relations1 everything works fine. Also, adding ToList() (which is sometimes mentioned as solution) does not change anything.

What am I missing?


Solution

  • I've tried a similar case and get the same error while in Entity Framework exactly the same query runs fine. So I think you're running into a bug here (or an "unsupported feature") and you may have to work around it. You could do that by turning the query around:

    var relationQry =
          from relation in anchor.Relations
          where anchorIDs.Contains(relation.AnchorIDTo) 
             || anchorIDs.Contains(relation.AnchorIDFrom)
          select new
          {
              relationID = relation.RelationID,
              anchorIDFrom = relation.AnchorIDFrom,
              anchorIDTo = relation.AnchorIDTo,
              relationTypes = from type in relation.RelationTypes
                              select type.RelationType1
          };
    

    By the way: you can change the "stupid names" :) by selecting the association in the designer and the names of its child property.