Showing results for 
Search instead for 
Did you mean: 

Allow casting of entity in Linq where clause

Author Name: Morten Lemvigh

We have defined a number of interfaces for our proxy classes, one such is IGeneralStateCodeEntity used for entities with a Active/Inactive statecode. We have used these interfaces to write query abstractions like this:

public static IQueryable Active(this IQueryable records)
where TEntity : IGeneralStateCodeEntity
return records
.Where(record => record.statecode == GeneralStatecode.Active);

This allows us to write more expressive linq queries along the lines of :

This worked fine with our custom linq provider we used for CRM4. Now we are upgrating to CRM 2016 and this now breaks. Apparently the extension method above generates an expression similar to:
.Where(record => ((IGeneralStateCodeEntity)record).statecode == GeneralStatecode.Active);
And that breaks the linq provider. It would be very nice, if the linq provider was able to diregard this casting.

Status: New
Regular Visitor
Status changed to: New