Tuesday, June 13, 2017

Querying Entity collection using LINQ

EntityCollection resulttemp = getContactEntityCollection(erRequest.Id, service);
                                if (resulttemp != null)
                                {
                                    List<Entity> listGCRQue = resulttemp.Entities.Where(e => (e.Contains("mms_reason") && ((OptionSetValue)e["OptionsetSchemaname"]).Value == 100000148 || ((OptionSetValue)e["OptionsetSchemaname"]).Value == 100000149 || ((OptionSetValue)e["OptionsetSchemaname"]).Value == 100000150 || ((OptionSetValue)e["OptionsetSchemaname"]).Value == 100000151)).ToList();
                                    List<Entity> listNonGCRQue = resulttemp.Entities.Where(e => (e.Contains("OptionsetSchemaname") && ((OptionSetValue)e["mms_reason"]).Value != 100000148 && ((OptionSetValue)e["OptionsetSchemaname"]).Value != 100000149 && ((OptionSetValue)e["OptionsetSchemaname"]).Value != 100000150 && ((OptionSetValue)e["OptionsetSchemaname"]).Value != 100000151)).ToList();

                                    if ((listGCRQue.Count > 0) || (listGCRQue.Count == 0 && listNonGCRQue.Count != 0 && (reasonValue == 100000148 || reasonValue == 100000149 || reasonValue == 100000150 || reasonValue == 100000151)))
                                    {
                                        throw new InvalidPluginExecutionException(OperationStatus.Canceled, "Error message");
                                    }
                                }


public EntityCollection getContactEntityCollection(Guid reqId, IOrganizationService service)
        {
            string fetchXml = "";
            fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                    "<entity name='incident'>" +                  
                    "<attribute name='OptionsetSchemaname' />" +
                    "<attribute name='incidentid' />" +
                    "<filter type='and'>" +
                    "<condition attribute='mms_requestid' operator='eq' value='" + reqId + "' />" +
                    "</filter>" +
                    "</entity>" +
                    "</fetch>";

            EntityCollection resulttemp = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (resulttemp != null && resulttemp.Entities.Count > 0)
                return resulttemp;
            else
                return null;
        }

No comments:

Post a Comment