After reading around I found that I could not use the .Contains method when querying CRM 2011 with the SDK Linq provider. This is a problem as I need to query for records which belong to a collection of Guid’s that get passed from my application. After more investigating I discovered LinqKit and the Predicate Builder; great! Using a single entityset in a query the predicate works fine:
var visits = Context.Crm_scheduled_visitSet.AsExpandable().Where(predicate);
The problem I have is when I expand the query to include a join to another entityset I cannot get the query to run when I include the predicate.
This is my query without the predicate which runs fine but I need to filter the Crm_scheduled_visitSet on a collection of Guid’s:
var scores = Context.Crm_scheduled_visitSet
.Join(Context.crm_business_risk_scoreSet, v => v.Crm_scheduled_visitId.Value, s => s.crm_business_risk_score_id.Id, (v, s) => new
{
OrgID = v.Crm_client_id.Id,
VisitStartdate = object.Equals(null, v.crm_actual_start_date) ? new DateTime() : v.crm_actual_start_date.Value,
SectionId = s.crm_business_risk_section_id.Id,
RisLevelId = s.crm_business_risk_level_id.Id,
RiskRatingId = s.crm_business_risk_rating_id.Id
});
What I need to do but this doesn’t run:
var scores = base.Context.Crm_scheduled_visitSet.AsExpandable().Where(predicate)
.Join(base.Context.crm_business_risk_scoreSet, v => v.Crm_scheduled_visitId.Value, s => s.crm_business_risk_score_id.Id, (v, s) => new
{
OrgID = v.Crm_client_id.Id,
VisitStartdate = object.Equals(null, v.crm_actual_start_date) ? new DateTime() : v.crm_actual_start_date.Value,
SectionId = s.crm_business_risk_section_id.Id,
RisLevelId = s.crm_business_risk_level_id.Id,
RiskRatingId = s.crm_business_risk_rating_id.Id
});
I could not get this to work, resorted to using query expressions. I hope the CRM sdk Linq provider is improved in the future be more in line with features offered by Linq to Entities.