使用LINQ根据单个属性过滤EntityCollection:

时间:2019-01-02 21:00:13

标签: c# linq dynamics-crm dynamics-crm-online dynamics-365

我有一个由fetchXML查询组成的实体集合。

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));

我需要基于“ new_productid”属性过滤上述实体集合。如果两个实体的'new_productid'值相同,那么我只希望将其中一个实体呈现为新的过滤后的实体集合。我将需要new_productid和new_subquantity和new_rlsid放入我的新的实体集合中。

我注意到LINQ具有.Distinct()属性,但是CRM的所有msdn示例都显示了如何使用早期绑定对LINQ进行此操作。我所有的插件都是后期绑定,需要使用Linq解决方案进行后期绑定。

1 个答案:

答案 0 :(得分:1)

此示例显示了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();
}