1
votes

I'm having problems saving an entity which I have gotten from CRM using a LINQ query containing a join.

The inner exception I get when trying to update the CRM entity is:

Message = "Message = "'x_productpurchase' entity doesn't contain attribute with Name = 'l_0.statuscode'."

The query:

var subscriptionsTemp = (from p in _serviceContext.x_productpurchaseSet
         join l in _serviceContext.x_ProductInOrderSet on p.Id equals l.x_ProductsInOrder.Id
         where p.x_contactId.Id == contactID
             && p.x_subscription_type != null
         select new { subsciption = p, line = l }).ToList();

List<x_productpurchase> subscriptions = subscriptionsTemp
        .Where(x => productTypesToLookFor == null || (x.line.x_productId != null && productTypesToLookFor.Contains(x.line.x_productId.Id)))
        .Select(x => x.subsciption).Distinct().ToList<x_productpurchase>();

The purpose of the above code is to retrieve product orders for a certain contact containing specific products defined in List<Guid> productTypesToLookFor.

After we have gotten a row back we change a few values on the retrieved entities and try to save the changes back to CRM using _serviceContext.Update() // XrmServiceContext.

This is when the above error is thrown.

We have come to the conclusion that the error is caused by the Join of a sub table in the query as this seems to introduce an alias in some of the variable names that are returned. This can be seen in the attributes of a retrieved entity (green circles): Alias

Any ideas as to get rid of this problem so we can save and work with the entities right away?

Right now we are solving the problem by doing the above query and then getting the specific product order again using its Guid and then working on that. Not an ideal situation as this forces us to do more database queries.

1
we change a few values did you change statecode or statuscode fields? - Guido Preite
No, we only changed a few custom fields which we have added ourselves. - JensB
If you put a breakpoint on the first line you show, does _serviceContext.x_productpurchaseSet[0].Attributes already contain those that are causing you problems, i.e. the l_0 AliasedValues? - p e p
Yes, they are already there. I believe it is the join in the first statement causing them as if this is removed they are gone. - JensB

1 Answers

2
votes

Very interesting problem. I normally work with Query Expressions and not Linq Statements, so I'm a little impressed with the Linq Provider working here and the magic it is performing to make it look like you can select two different types in a single query.

Using the CRM SDK, only a single type of entity can be returned. When you join to another entity and return attributes from it, it gets added to the first entity as AliasedValues.

I would create your own Save Extension Method that loops through the attribute collection, and removes any item from the collection who's value is of type AliasedValue (you could potentially remove any item with a key that contains a "." in it (since it can't be a valid attribute name for the)).