0

I have this code inside my .net core console application which uses PnP core SDK:-

var workordersparepartdetailsList = context.Web.Lists.GetByTitle("Work Orders Spare Part Details", p => p.Title,
                                                                p => p.Fields.QueryProperties(p => p.InternalName,
                                                                                              p => p.FieldTypeKind,
                                                                                              p => p.TypeAsString,
                                                                                              p => p.Title));

            // Build a query that only returns these fields for the Work Orders
            string viewXml4 = @"<View Scope='RecursiveAll'>
                    <ViewFields>
                      <FieldRef Name='Title' />
                      <FieldRef Name='Created' />
                      <FieldRef Name='FileLeafRef' />
                        <FieldRef Name='AssetID' />
                        <FieldRef Name='SparePartApprovalStatusSync' />
                        <FieldRef Name='IssueTypeID' />
                        <FieldRef Name='SparePartIDSync' />
                    </ViewFields>
                    <Query>
                    <Where>
                    <And>
                    <Eq>
                        <FieldRef Name='FSObjType' />
                        <Value Type='Integer'>0</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='SparePartApprovalStatusSync' />
                        <Value Type='text'>Approved</Value>
                    </Eq>
                    </And>
                    </Where>
                    </Query>
                   <RowLimit Paged='TRUE'>5000</RowLimit>
                   </View>";

            // Load all the needed data using paged requests
            bool paging4 = true;
            string nextPage4 = null;
            while (paging4)
            {
                var output = await workordersparepartdetailsList.LoadListDataAsStreamAsync(new RenderListDataOptions()
                {
                    ViewXml = viewXml4,
                    RenderOptions = RenderListDataOptionsFlags.ListData,
                    Paging = nextPage4 ?? null,
                }).ConfigureAwait(false);

                if (output.ContainsKey("NextHref"))
                {
                    nextPage4 = output["NextHref"].ToString().Substring(1);
                }
                else
                {
                    paging4 = false;
                }
            }

            //++Assets KPI    
            var WOSPDByAssetSP = workordersparepartdetailsList.Items.AsRequested().
            Where(a => a["AssetID"] != null && a["AssetID"].ToString() != string.Empty && a["SparePartApprovalStatusSync"] != null &&  a["SparePartApprovalStatusSync"].ToString() != System.String.Empty && a["SparePartApprovalStatusSync"].ToString() == "Approved")
 .GroupBy(wo => new { wo.AssetID, wo.Year, wo.MediumID })
           .Select(wospd =>
           {
               // Attempt to safely get each property, providing defaults for missing or null values

               var WOSPDStatus = wospd["SparePartApprovalStatusSync"] != null ? wospd["SparePartApprovalStatusSync"].ToString() : string.Empty;
               var assetId = wospd["AssetID"] != null ? wospd["AssetID"].ToString() : null;
               var createdStr = wospd["Created"] != null ? wospd["Created"].ToString() : null;
               var mediumID = !string.IsNullOrEmpty(assetId) && AssetIdMediumMap.TryGetValue(assetId, out var mid) ? mid : string.Empty;
               var sparepartID = wospd["SparePartIDSync"] != null ? wospd["SparePartIDSync"].ToString() : string.Empty;
               DateTime.TryParse(createdStr, out DateTime created); // Defaults to DateTime.MinValue if parsing fails
               return new
               {
                   WOSPDStatus = WOSPDStatus,
                   Year = created.Year,
                   Month = created.Month.ToString(),
                   MediumID = mediumID,
                   AssetID = assetId,
                   SparePartID = sparepartID
               };
           }).ToList();

Now first I am getting all the data using paging inside workordersparepartdetailsList list, then i am doing a group by and build another list named WOSPDByAssetSP. so, I think at the end I will have 2 copies of the list items; one inside the workordersparepartdetailsList list and the other inside WOSPDByAssetSP list. To better utilize the memory usage, how I can clear the workordersparepartdetailsList list once I build the WOSPDByAssetSP list? as seems i cannot do something as follow workordersparepartdetailsList.Clear()? any advice please?

Thanks

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.