如果FetchXML(AGGREGATE SUM)查询未找到任何内容,那么EntityCollection返回什么?

时间:2018-07-23 19:32:36

标签: plugins microsoft-dynamics fetchxml

我正在运行一个FetchXML查询(对于fetch来说为aggregate ='true',对于所有属性来说是aggregate ='sum'),并将其传递给我的C#插件代码中的RetreiveMultiple。如果Fetchxml找不到任何东西,在这种情况下service.RetrieveMultiple会检索什么? 在调试器中,存在NullReference异常

FetchXML Builder返回以下内容: Querying a meal contract line that has not been created!!

我需要处理合同中缺少一餐的情况...在我的插件中,我有三个Fetchxml表达式,它们从合同中的实体中检索数据(一天中每顿饭,一个用于AM SNACKS /午餐/下午零食)。如果FetchXMl实体之一没有要检索的数据,则插件根本不会产生任何东西...即使其中一个FetchXML查询返回了一些结果。.我只是想在没有数据返回的情况下放置零。三个FETCHXML查询中的任何一个... 请注意,如果我在合同行中包含所有三餐,则插件工作正常!这是我的代码:

try
                {
                    //Get Contract StartDate                   
                    DateTime startDate = (DateTime)PreImage["activeon"];

                    //Get Contract EndDate
                     DateTime endDate = (DateTime)PreImage["expireson"];

                    //Create an instance of the range class
                    Eachday range = new Eachday();

                    //use Weekday method of range class to get a weekdays list
                    var weekdays = range.WeekDay(startDate, endDate);                               

                    //Get Contract Number
                    string contractNumber = (string)PreImage["contractnumber"];

                    //Get Unit Order Lookup Id
                    EntityReference unitOrder = (EntityReference)PreImage.Attributes["new_unitorderid"];
                    var unitOrderId = unitOrder.Id;


                    //Query and aggregate each Weekday's order for the 3 different meal times...

                    //AM SNACK FetchXML Query.. string is passed to service.RetrieveMultiple();
                    string unitsum_am = @"  <fetch aggregate='true' distinct='false' >
                        <entity name='contract' >
                            <link-entity name='contractdetail' from = 'contractid' to = 'contractid' >
                                <attribute name='new_mondayunits' alias='new_mondayunits_amsum' aggregate='sum' />
                                <attribute name='new_tuesdayunits' alias='new_tuesdayunits_amsum' aggregate='sum' />
                                <attribute name='new_unitswednesday' alias='new_unitswednesday_amsum' aggregate='sum' />                       
                                <attribute name='new_unitsthursday' alias='new_unitsthursday_amsum' aggregate='sum' />                            
                                <attribute name='new_unitsfriday' alias='new_unitsfriday_amsum' aggregate='sum' />
                                <filter type='and' >
                                    <condition value='100000001' attribute='new_servingtime' operator= 'eq' />                                       
                                    <condition value='0' attribute='statecode' operator= 'eq' />
                                    <condition value='" + targetId + @"' attribute='contractid' operator= 'eq' />
                                </filter >

                            </link-entity> 
                         </entity >
                     </fetch>";


                    //Retrieve a collection of records
                    EntityCollection unitsum_am_result =
                        service.RetrieveMultiple(new FetchExpression(unitsum_am));

                    //AM SNACKS list for the week
                    var am_list = new List<int>();

                    //iterate through all records and get the sum for each day
                    //place the sums into the am_list
                    if (unitsum_am_result.Entities.Count == 0)
                    {
                        am_list.AddRange(Enumerable.Repeat(0, 5));
                    }
                    else
                    {
                        foreach (var unit in unitsum_am_result.Entities)
                        {
                            var mondaysum = ((int)((AliasedValue)unit["new_mondayunits_amsum"]).Value);
                            am_list.Add(mondaysum);
                            var tuesdaysum = ((int)((AliasedValue)unit["new_tuesdayunits_amsum"]).Value);
                            am_list.Add(tuesdaysum);
                            var wednesdaysum = ((int)((AliasedValue)unit["new_unitswednesday_amsum"]).Value);
                            am_list.Add(wednesdaysum);
                            var thursdaysum = ((int)((AliasedValue)unit["new_unitsthursday_amsum"]).Value);
                            am_list.Add(thursdaysum);
                            var fridaysum = ((int)((AliasedValue)unit["new_unitsfriday_amsum"]).Value);
                            am_list.Add(fridaysum);
                        }
                    }





                    //LUNCH FetchXML Query.. string is passed to service.RetrieveMultiple();
                    string unitsum_lunch = @"   <fetch aggregate='true' distinct='false' >
                        <entity name='contract' >
                            <link-entity name='contractdetail' from = 'contractid' to = 'contractid' >
                                <attribute name='new_mondayunits' alias='new_mondayunits_lunchsum' aggregate='sum' />
                                <attribute name='new_tuesdayunits' alias='new_tuesdayunits_lunchsum' aggregate='sum' />
                                <attribute name='new_unitswednesday' alias='new_unitswednesday_lunchsum' aggregate='sum' />                       
                                <attribute name='new_unitsthursday' alias='new_unitsthursday_lunchsum' aggregate='sum' />                            
                                <attribute name='new_unitsfriday' alias='new_unitsfriday_lunchsum' aggregate='sum' />
                                <filter type='and' >
                                    <condition value='100000002' attribute='new_servingtime' operator= 'eq' />                                       
                                    <condition value='0' attribute='statecode' operator= 'eq' />
                                    <condition value='" + targetId + @"' attribute='contractid' operator= 'eq' />
                                </filter >                                          
                            </link-entity> 
                         </entity >
                     </fetch>";


                    //Retrieve a collection of records
                    EntityCollection unitsum_lunch_result =
                        service.RetrieveMultiple(new FetchExpression(unitsum_lunch));

                    //Lunch meals list for the week
                    var lunch_list = new List<int>();

                    //iterate through all records and get the sum for each day
                    //place the sums into the lunch_list
                    if (unitsum_lunch_result.Entities.Count == 0)
                    {
                        lunch_list.AddRange(Enumerable.Repeat(0, 5));
                    }
                    else
                    {
                        foreach (var unit in unitsum_lunch_result.Entities)
                        {
                            var mondaysum = ((int)((AliasedValue)unit["new_mondayunits_lunchsum"]).Value);
                            lunch_list.Add(mondaysum);
                            var tuesdaysum = ((int)((AliasedValue)unit["new_tuesdayunits_lunchsum"]).Value);
                            lunch_list.Add(tuesdaysum);
                            var wednesdaysum = ((int)((AliasedValue)unit["new_unitswednesday_lunchsum"]).Value);
                            lunch_list.Add(wednesdaysum);
                            var thursdaysum = ((int)((AliasedValue)unit["new_unitsthursday_lunchsum"]).Value);
                            lunch_list.Add(thursdaysum);
                            var fridaysum = ((int)((AliasedValue)unit["new_unitsfriday_lunchsum"]).Value);
                            lunch_list.Add(fridaysum);
                        }
                    }



                    //PM SNACK FetchXML Query.. string is passed to service.RetrieveMultiple();
                    string unitsum_pm = @"  <fetch aggregate='true' distinct='false' >
                        <entity name='contract' >
                            <link-entity name='contractdetail' from = 'contractid' to = 'contractid' >
                                <attribute name='new_mondayunits' alias='new_mondayunits_pmsum' aggregate='sum' />
                                <attribute name='new_tuesdayunits' alias='new_tuesdayunits_pmsum' aggregate='sum' />
                                <attribute name='new_unitswednesday' alias='new_unitswednesday_pmsum' aggregate='sum' />                       
                                <attribute name='new_unitsthursday' alias='new_unitsthursday_pmsum' aggregate='sum' />                            
                                <attribute name='new_unitsfriday' alias='new_unitsfriday_pmsum' aggregate='sum' />
                                <filter type='and' >
                                    <condition value='100000003' attribute='new_servingtime' operator= 'eq' />                                       
                                    <condition value='0' attribute='statecode' operator= 'eq' />
                                    <condition value='" + targetId + @"' attribute='contractid' operator= 'eq' />
                                </filter >                                          
                            </link-entity> 
                         </entity >
                     </fetch>";

                    //Retrieve a collection of records
                    EntityCollection unitsum_pm_result =
                        service.RetrieveMultiple(new FetchExpression(unitsum_pm));


                    //PM SNACK list for the week
                    var pm_list = new List<int>();

                    //iterate through all records and get the sum for each day
                    //place the sums into the pm_list

                    if (unitsum_pm_result.Entities.Count == 0)
                    {
                        pm_list.AddRange(Enumerable.Repeat(0, 5));
                    }
                    else
                    {
                        foreach (var unit in unitsum_pm_result.Entities)
                        {
                            var mondaysum = ((int)((AliasedValue)unit["new_mondayunits_pmsum"]).Value);
                            pm_list.Add(mondaysum);
                            var tuesdaysum = ((int)((AliasedValue)unit["new_tuesdayunits_pmsum"]).Value);
                            pm_list.Add(tuesdaysum);
                            var wednesdaysum = ((int)((AliasedValue)unit["new_unitswednesday_pmsum"]).Value);
                            pm_list.Add(wednesdaysum);
                            var thursdaysum = ((int)((AliasedValue)unit["new_unitsthursday_pmsum"]).Value);
                            pm_list.Add(thursdaysum);
                            var fridaysum = ((int)((AliasedValue)unit["new_unitsfriday_pmsum"]).Value);
                            pm_list.Add(fridaysum);
                        }
                    }

                    //iterate through entire contract duration
                    foreach(var day in weekdays)
                    {
                        Entity alterunit = new Entity("new_alterunitorder");                   //create new alter unit record
                        alterunit.Attributes.Add("new_orderdate", DateTime.Parse(day));        //set the day field
                        var currentday = day.Split(',')[0];                                    //current day

                        switch (currentday)
                        {
                            case "Monday":
                                alterunit.Attributes.Add("new_amsnack", am_list[0]);        //set the total am snacks
                                alterunit.Attributes.Add("new_lunch", lunch_list[0]);       //set the total lunch meals
                                alterunit.Attributes.Add("new_pmsnack", pm_list[0]);        //set the total pm snacks
                                break;
                            case "Tuesday":
                                alterunit.Attributes.Add("new_amsnack", am_list[1]);
                                alterunit.Attributes.Add("new_lunch", lunch_list[1]);
                                alterunit.Attributes.Add("new_pmsnack", pm_list[1]);
                                break;
                            case "Wednesday":
                                alterunit.Attributes.Add("new_amsnack", am_list[2]);
                                alterunit.Attributes.Add("new_lunch", lunch_list[2]);
                                alterunit.Attributes.Add("new_pmsnack", pm_list[2]);
                                break;
                            case "Thursday":
                                alterunit.Attributes.Add("new_amsnack", am_list[3]);
                                alterunit.Attributes.Add("new_lunch", lunch_list[3]);
                                alterunit.Attributes.Add("new_pmsnack", pm_list[3]);
                                break;
                            case "Friday":
                                alterunit.Attributes.Add("new_amsnack", am_list[4]);
                                alterunit.Attributes.Add("new_lunch", lunch_list[4]);
                                alterunit.Attributes.Add("new_pmsnack", pm_list[4]);
                                break;
                            default:
                                Console.WriteLine($"An unexpected value ({currentday})");
                                break;

                        }

                        alterunit.Attributes.Add("new_name", contractNumber);                   //set the record name

                        //set the unit order record relation
                        alterunit.Attributes["new_orderlineid"] =                               
                            new EntityReference("new_units", unitOrderId);             
                        service.Create(alterunit);                                              //create the record
                    }



                } 

这是调试的结果:

System.NullReferenceException

第190行也引发了异常:

debugger!!

3 个答案:

答案 0 :(得分:1)

它将返回完全限定的EntityCollection,但是Entities属性在集合中将有零个记录。

答案 1 :(得分:0)

既不为null也不为空。

您可以使用以下代码段识别结果集:

if (unitsum_am_result.Entities.Count == 0)
{
    am_list.AddRange(Enumerable.Repeat(0, 5));
}
else
{
    //foreach....
}

答案 2 :(得分:0)

实际上,实体集合会返回完全合格的EntityCollection,并且实体计数将为零。但是,似乎在使用聚合'sum'时,如果集合为空,则平台将引发错误。 “计数”不会发生这种情况。为了解决这个问题,我做到了:

//iterate through all records and get the sum for each day
                    //place the sums into the am_list
                    foreach(var unit in unitsum_am_result.Entities)
                    {
                        try
                        {
                            am_list.Add(((int)((AliasedValue)unit["new_mondayunits_amsum"]).Value));
                            am_list.Add(((int)((AliasedValue)unit["new_tuesdayunits_amsum"]).Value));
                            am_list.Add(((int)((AliasedValue)unit["new_unitswednesday_amsum"]).Value));
                            am_list.Add(((int)((AliasedValue)unit["new_unitsthursday_amsum"]).Value)); 
                            am_list.Add(((int)((AliasedValue)unit["new_unitsfriday_amsum"]).Value));
                        }
                        catch
                        {
                            am_list.AddRange(Enumerable.Repeat(0, 5));
                        }
                    }