0
votes

I have an entitycollection composed from a fetchXML query.

string fetch = @"
  <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
     <entity name='new_rl'>
        <attribute name='new_rlsid' />
        <attribute name='new_productid' />
        <attribute name='new_subquantity' />
        <filter type='and'>
           <condition attribute='new_sg' operator='eq' value='" + servingGroup.Value.ToString() + @"' />
           <condition attribute='new_s' operator='eq' value='' />
           <condition attribute='new_ld' operator='eq' value='" + location + @"' />
           <condition attribute='new_productid' operator='not-null' />
        </filter>
        <link-entity name='new_ys' from='new_ysid' to='new_orderid' link-type='inner' alias='ae'>
        <filter type='and'>
           <condition attribute='new_ysid' operator='eq' value='{" + yGuid.ToString() + @"}' />
        </filter>
      </link-entity>
    </entity>
  </fetch>";


EntityCollection Labels = service.RetrieveMultiple(new FetchExpression(fetch));

I need to filter the above entitycollection based on the 'new_productid' attribute. If two entities have the same value for 'new_productid', then I only want one of those entities to be rendered into a new filtered entity collection. I would need new_productid and new_subquantity and new_rlsid to be in my new entitycollection.

I noticed that LINQ has the .Distinct() property, but all the msdn examples for CRM show how to do this with LINQ using early binding. All of my plugins are late bound and need a Linq solution for late binding.

1

1 Answers

1
votes

This example shows late bound with LINQ:

using (var context = new OrganizationServiceContext(service))
{
    var result = (from a in context.CreateQuery("account")
                    join o in context.CreateQuery("opportunity")
                    on a.GetAttributeValue<Guid>("accountid") equals o.GetAttributeValue<Guid>("new_officeid")
                    where a.GetAttributeValue<OptionSetValue>("statecode").Value == 0
                    where o.GetAttributeValue<Guid>("parentaccountid") != Guid.Empty
                    orderby a.GetAttributeValue<string>("name")
                    select new KeyValuePair<Guid, string>(a.GetAttributeValue<Guid>("accountid"), a.GetAttributeValue<string>("name")))
                    .Distinct();
}