CRM 2011 GROUP and COUNT

烈酒焚心 提交于 2019-12-04 18:43:14
Peter Majeed

This topic has come up before, but the basic answer is that you can't do this via LINQ, but you can via Microsoft's FetchXML. In fact, the first example on GROUP BY in the SDK addresses the requirements of your example about as perfectly as any SDK can.

Try this:

var linqQuery =
    from r in orgServiceContext.CreateQuery("opportunity")
    join c in orgServiceContext.CreateQuery("systemuser") on ((EntityReference)r["ownerid"]).Id equals c["systemuserid"]
    group r by ((EntityReference)r["ownerid"]).Id into oop
    select new
    {
      Key = oop.Key,
      Count = oop.Count(),
      Opportunities = oop //contains the list of opportunities grouped by OwnerId
    };

Opportunities contains a collection of IGrouping You must iterate through the collection IEnumerable<Microsoft.Xrm.Sdk.Entity> to create the list of opportunities with the members you are interested. (the projection you made by using the new operator)

var outputOpportunities = new ArrayList();
foreach (IGrouping<Guid, Microsoft.Xrm.Sdk.Entity> group in linqQuery)
{
    foreach (Microsoft.Xrm.Sdk.Entity opportunity in group)
        outputOpportunities.Add
        (new
            {
                OpportunityId = !opportunity.Contains("opportunityid") ? string.Empty : opportunity["opportunityid"],
                CustomerId = !opportunity.Contains("customerid") ? string.Empty : ((EntityReference)opportunity["customerid"]).Name,
                Priority = !opportunity.Contains("opportunityratingcode") ? string.Empty : opportunity.FormattedValues["opportunityratingcode"],
                ContactName = !opportunity.Contains("new_contact") ? string.Empty : ((EntityReference)opportunity["new_contact"]).Name,
                Source = !opportunity.Contains("new_source") ? string.Empty : ((String)opportunity["new_source"]),
                CreatedOn = !opportunity.Contains("createdon") ? string.Empty : ((DateTime)opportunity["createdon"]).ToShortDateString(),
                Eval = !opportunity.Contains("new_distributorevaluation") || ((OptionSetValue)opportunity["new_distributorevaluation"]).Value.ToString() == "100000000" ? "NA" : opportunity.FormattedValues["new_distributorevaluation"].Substring(0, 2),
                EvalVal = !opportunity.Contains("new_distributorevaluation") ? "100000000" : ((OptionSetValue)opportunity["new_distributorevaluation"]).Value.ToString(),
                DistributorName = !opportunity.Contains("new_channelpartner") ? string.Empty : ((EntityReference)opportunity["new_channelpartner"]).Name,
                Notes = !opportunity.Contains("new_distributornotes") ? string.Empty : opportunity["new_distributornotes"],
                EstimatedCloseDate = !opportunity.Contains("estimatedclosedate") ? string.Empty : opportunity["estimatedclosedate"],
                MaturityValue = !opportunity.Contains("estimatedvalue") ? string.Empty : ((Money)opportunity["estimatedvalue"]).Value.ToString(),
                SentToDistributorOn = !opportunity.Contains("new_senttodistributoron") ? DateTime.MinValue.ToShortDateString() : ((DateTime)opportunity["new_senttodistributoron"]).ToShortDateString(),
                LeadStatus = !opportunity.Contains("new_leadstatus") ? string.Empty : ((OptionSetValue)opportunity["new_leadstatus"]).Value.ToString(),
                EmailedToRSM = !opportunity.Contains("new_emailedtorsm") ? string.Empty : opportunity.FormattedValues["new_emailedtorsm"],
                EmailedToDistributor = !opportunity.Contains("new_emailedtodistributor") ? string.Empty : opportunity.FormattedValues["new_emailedtodistributor"],
                Owner = !opportunity.Contains("ownerid") ? string.Empty : ((EntityReference)opportunity["ownerid"]).Name,
                OwnerEmail = !opportunity.Contains("internalemailaddress") ? string.Empty : ((String)opportunity["internalemailaddress"])
            }
        );
}        

Basically i took your query and added the group by operator and in the projection statement i put the count by ownerid.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!