I'm using the XRM (early bound) types in a WCF project so I have access to the CRM model and can use LINQ queries. But I've been running into a problem described here, it's the limitations on the where
clause specific to XRM LINQ:
where [clause limitations]
The left side of the clause must be an attribute name and the right side of the clause must be a value. You cannot set the left side to a constant. Both the sides of the clause cannot be constants.
Supports the String functions Contains, StartsWith, EndsWith, and Equals.
One requirement that keeps popping up is when a parameter is null, all entities should be returned otherwise filter by the parameter. But I can't think of a way to do this without breaking the requirements above, or writing multiple queries to handle the scenario when it's null.
this is an example of one of my queries, the typeFilter == null
is the problem here is I've used a constant on the LHS. In my real code there's a guard clause that points typeFilter == null
to another query but I now have to add a start/end date filter (both nullable) and I cannot express how much I don't want to write a query for every combination of nullables.
private IQueryable<EventInfo> getAllEvents( DataContext context, EventType? typeFilter )
{
return (
from evt in context.new_eventSet
where
( evt.statecode == new_eventState.Active ) &&
( typeFilter == null || evt.new_EventType.Value == (int) typeFilter.Value )
select new EventInfo()
{
ID = evt.Id,
EventType = (EventType) evt.new_EventType.Value
...
} );
}
How about:
If you want to use the Linq syntax, it is possible to construct a query dynamically using LinqKit.
I have used it for this purpose on the Dynamics CRM project which I'm currently working on, and it does the job very well.
Please refer to the following answer, which is where I got the idea: https://stackoverflow.com/a/5152946/344988
I've answered my own question! sometimes you just need a place to vent your problems before you get it.
the trick was to not use LINQ syntax: