EntityCollection return if FetchXML (AGGREGATE SUM) Query didn't find anything 是什么意思?

What does EntityCollection return if FetchXML (AGGREGATE SUM) Query didn't find anything?

我是 运行 FetchXML 查询(聚合='true' 用于提取,聚合='sum' 用于所有属性)并在我的 C# 插件代码中将其传递给 RetreiveMultiple。如果 Fetchxml 没有找到任何东西怎么办,在这种情况下 service.RetrieveMultiple 检索什么? 在调试器中,出现 NullReference 异常

FetchXML 生成器 returns 这个:

我需要处理合同行中的一顿饭丢失的情况...在我的插件中,我有三个 Fetchxml 表达式,它们从合同行实体中检索数据(一天中的每顿饭,上午 SNACKS/LUNCH/PM 零食)。如果其中一个 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
                    }



                } 

调试结果如下:

190行也抛出异常:

它将 return 一个完全合格的 EntityCollection,但是 Entities 属性 在集合中将有零条记录。

既不为空也不为空。

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

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

实体集合实际上 return 是一个完全合格的 EntityCollection,实体计数将为零。但是,似乎在使用聚合 'sum' 时,如果集合为空,平台会抛出错误。 'count' 不会发生这种情况。为了修复它,我做了:

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