c# - Nullable filters in XRM where clause -


i'm using xrm (early bound) types in wcf project have access crm model , can use linq queries. i've been running a problem described here, it's limitations on where clause specific xrm linq:

where [clause limitations]

the left side of clause must attribute name , right side of clause must value. cannot set left side constant. both sides of clause cannot constants.

supports string functions contains, startswith, endswith, , equals.

one requirement keeps popping when parameter null, entities should returned otherwise filter parameter. can't think of way without breaking requirements above, or writing multiple queries handle scenario when it's null.

this example of 1 of queries, typefilter == null problem here i've used constant on lhs. in real code there's guard clause points typefilter == null query have add start/end date filter (both nullable) , cannot express how don't want write query every combination of nullables.

private iqueryable<eventinfo> getallevents( datacontext context, eventtype? typefilter ) {     return (         evt in context.new_eventset                 ( 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 (typefilter == null) {         return (         evt in context.new_eventset                 ( evt.statecode == new_eventstate.active )                select new eventinfo()         {             id = evt.id,             eventtype = (eventtype) evt.new_eventtype.value             ...         } );    } else {         return (         evt in context.new_eventset                 ( evt.statecode == new_eventstate.active ) &&         evt.new_eventtype.value == (int) typefilter.value )         select new eventinfo()         {             id = evt.id,             eventtype = (eventtype) evt.new_eventtype.value             ...         } );    } 

Comments