Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

I was having the same problem. Eventually I solved this.

It appears that even when the resulting types are equal, using nested relations will cause errors.

What I did to solve this problem was:

I defined DataLoadOptions as such:

var lo = new DataLoadOptions();

lo.LoadWith<Type>(t => t.NESTEDTYPE); // "t" is the database object in this scenario.
lo.LoadWith<Type>(t => t.NESTEDTYPE2);
lo.LoadWith<Type>(t => t.NESTEDTYPE3);

// etc.

dc.LoadOptions = lo;

For each nested type you have to specify a loadwith. You can only assign Loadoptions once on each "alive" datacontext.

I hope with this you will be able to resolve your problem!

Have you tried this:

from relation in anchor.Relations.Concat(anchor.Relations1)

or

from relation in anchor.Relations.Concat(anchor.Relations1).Distinct()

This 2nd should be functionally the same as Union, but I'm not convinced it is what you want, if it is it might give a better error message.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top