'C#, Newtonsoft, need to get array items not already handled

I have a json array that looks like...

  {
    "equipment": [{
        "date_of_examination": "2022-05-20T14:08:38.072965",
        "defect_type": ["DN"],
        "eqpt_ref": "AA1",
        "eqpt_name": ["2 Leg Chain Sling"],
        "eqpt_manufacturer": "Merc",
        "eqpt_model": "edes",
        "part_no": "A1",
        "serial_no": "A1",
        "year": "2019",
        "swl": "32 tons",
        "exam_type": ["6 Months"],
        "date_of_last_examination": "2021-11-20T00:00:00",
        "date_of_next_examination": "2022-11-20T00:00:00",
        "defect": "sling is torn",
        "action_required": "replace"
    }, {
        "date_of_examination": "2022-05-20T14:12:23.997004",
        "eqpt_ref": "AA2",
        "eqpt_name": ["Other - "],
        "eqpt_name_other": "widget",
        "eqpt_manufacturer": "merc",
        "eqpt_model": "edes",
        "part_no": "B1",
        "serial_no": "B1",
        "year": "2019",
        "swl": "32 tons",
        "exam_type": ["6 Months"]
    }, {
        "date_of_examination": "2022-05-20T14:13:24.795136",
        "defect_type": ["DF"],
        "eqpt_ref": "CC1",
        "eqpt_name": ["Endless Round Sling (2.5m)"],
        "eqpt_manufacturer": "merc",
        "eqpt_model": "edes",
        "part_no": "c1",
        "serial_no": "c1",
        "year": "2019",
        "swl": "42 tons",
        "exam_type": ["6 Months"],
        "defect": "stitching is coming undone",
        "danger_value": "6",
        "danger_units": ["Weeks"],
        "action_required": "needs to be stitched again"
    }]
}

I am attempting to loop through the array and filter items as I need, to populate a table later.

The table has three parts.

First, is show all items with a defect_type of "DN". Second is to show all defect_type of "DF", and the last part is to show all the rest (in his case, the one with eqpt_name of AA2)

My original code is...

                for (int j = 0; j <= 2; j++)
                {
                    // Note, some table name parts won't have the "Filter..." aspect
                    // the string below will change depending on which loop we are in.
                    string[] tableNameParts = "TableStart:equipment:defectNow:Filter:defect_type=DN".Split(':');
                    
                    string tableNameJson = tableNameParts[1].Replace("»", "");
                    
                    var jsonRows = IncomingJson[tableNameJson];
                    
                    if (tableNameParts.Count() > 3)
                    {
                        // We probably have a filter set.

                        if (tableNameParts[3].Replace("»", "").ToLower() == "filter" && tableNameParts.Count() > 4)
                        {
                            // These values are not set in stone. It is what values have been set in the JSON, and then matched.
                            // for example... TableStart:<subform name>:<differentiator>:Filter:<field name>=<field value>

                            string[] FilterParts = tableNameParts[4].Split('=');
                            // Get the filter field and value to filter by

                            if (FilterParts.Count() > 1)
                            {
                                string FilterField = FilterParts[0].Replace("»", "");
                                string FilterValue = FilterParts[1].Replace("»", "");
                                
                                JArray filteredArray = new JArray();
                                
                                if (jsonRows[0].GetType() == typeof(JObject))
                                {
                                    //int loopCount = 0;
                                    
                                    foreach (JObject arrayObject in jsonRows) // Each group can have a set of arrays. (each  record has multiple sub records)
                                    //for (int i = 0; i < jsonRows.Count(); i++)
                                    {
                                        //JObject arrayObject = jsonRows[i];
                                        
                                        foreach (var objectItem in arrayObject)
                                        {
                                            string objectItemValue = string.Empty;
                                            
                                            if (objectItem.Value.GetType() == typeof(JArray))
                                            {
                                                foreach (var item in objectItem.Value)
                                                {
                                                    objectItemValue += item;
                                                }
                                            }
                                            else
                                            {
                                                objectItemValue = (string)objectItem.Value;
                                            }
                                            
                                            if (objectItem.Key == FilterField && objectItemValue == FilterValue)
                                            {
                                                // We need to save the item.
                                                filteredArray.Add(arrayObject);
                                                testArray.Add(arrayObject);
                                                
                                                //arrayObject["filtered"] = true;
                                                //IncomingJson[tableNameJson][loopCount]["filtered"] = true;
                                            }
                                            
                                        }
                                        //loopCount++;
                                    }
                                }
                                else
                                {
                                    foreach (JArray arrayGroup in jsonRows) // The array group (e.g. fault_record_subform)
                                    {
                                        // We are looking through the json array, to find any rows that match our filter key and filter value.
                                        // We will then add that into our jsonRows
                                        //int loopCount = 0;
                                        
                                        foreach (JObject arrayObject in arrayGroup) // Each group can have a set of arrays. (each  record has multiple sub records)
                                        {
                                            
                                            foreach (var objectItem in arrayObject)
                                            {
                                                string objectItemValue = string.Empty;
                                                
                                                if (objectItem.Value.GetType() == typeof(JArray))
                                                {
                                                    foreach (var item in objectItem.Value)
                                                    {
                                                        objectItemValue += item;
                                                    }
                                                }
                                                else
                                                {
                                                    objectItemValue = (string)objectItem.Value;
                                                }
                                                
                                                if (objectItem.Key == FilterField && objectItemValue == FilterValue)
                                                {
                                                    // We need to save the item.
                                                    filteredArray.Add(arrayObject);
                                                    testArray.Add(arrayObject);
                                                    //arrayObject["filtered"] = true;
                                                    //IncomingJson[tableNameJson][loopCount]["filtered"] = true;
                                                }
                                                
                                            }
                                        }
                                        //loopCount++;
                                    }
                                }
                                
                                //filteredArray.CopyTo(testArray, 0);
                                
                                jsonRows = filteredArray;   // limit the jsonRows to the filtered set (overwrite the jsonRows)
                                
                            }
                        }

                    }
                    else
                    {
                        // This is not a filter set
                        JArray singleArray = new JArray();

                        foreach(var arraySet in jsonRows)
                        {
                            if (!testArray.Intersect(arraySet).Any())
                            {
                                if (arraySet.GetType() == typeof(JObject))
                                {
                                    singleArray.Add(arraySet);
                                }
                                else
                                {
                                    foreach (JObject arrayObject in arraySet)
                                    {
                                        singleArray.Add(arrayObject);
                                    }
                                }
                            }
                        }

                        jsonRows = singleArray;
                    }
                    
                }                       

By the time it gets to the "this is not a filter set" (which should be the third iteration of the loop), I need to be able to ignore the other filtered items, but as you might see, I have attempted to mark an item as filtered (then filter out). I have also tried to add the filtered items to an alternative array and use that to filter out. All to no avail.

How do I make it so that the "this is not a filter set" rows can ignore the rows already filtered?

=========== EDIT ==============

After reviewing the link from dbc to the fiddler (I don't have an account on there, and don't know how to link to my changes), I have it running in the fiddler with the code below.

        JObject json = JObject.Parse(GetJson());

        string[] tableNames = {"TableStart:equipment:defectNow:Filter:defect_type=DN","TableStart:equipment:defectFuture:Filter:defect_type=DF","TableStart:equipment:defectNone"};
        
        for (int j = 0; j <= 2; j++)
        {
            // Note, some table name parts won't have the "Filter..." aspect
            // the string below will change depending on which loop we are in.
            
            string[] tableNameParts = tableNames[j].Split(':');

            string tableNameJson = tableNameParts[1].Replace("»", "");

            var jsonRows = json[tableNameJson];

            if (tableNameParts.Count() > 3)
            {
                // We probably have a filter set.

                if (tableNameParts[3].Replace("»", "").ToLower() == "filter" && tableNameParts.Count() > 4)
                {
                    // These values are not set in stone. It is what values have been set in the JSON, and then matched.
                    // for example... TableStart:<subform name>:<differentiator>:Filter:<field name>=<field value>

                    string[] FilterParts = tableNameParts[4].Split('=');
                    // Get the filter field and value to filter by

                    if (FilterParts.Count() > 1)
                    {
                        string FilterField = FilterParts[0].Replace("»", "");
                        string FilterValue = FilterParts[1].Replace("»", "");

                        JArray filteredArray = new JArray();

                        if (jsonRows[0].GetType() == typeof(JObject))
                        {
                            //int loopCount = 0;

                            foreach (JObject arrayObject in jsonRows) // Each group can have a set of arrays. (each  record has multiple sub records)
                                //for (int i = 0; i < jsonRows.Count(); i++)
                            {
                                //JObject arrayObject = jsonRows[i];

                                foreach (var objectItem in arrayObject)
                                {
                                    string objectItemValue = string.Empty;

                                    if (objectItem.Value.GetType() == typeof(JArray))
                                    {
                                        foreach (var item in objectItem.Value)
                                        {
                                            objectItemValue += item;
                                        }
                                    }
                                    else
                                    {
                                        objectItemValue = (string)objectItem.Value;
                                    }

                                    if (objectItem.Key == FilterField && objectItemValue == FilterValue)
                                    {
                                        // We need to save the item.
                                        filteredArray.Add(arrayObject);
                                        //testArray.Add(arrayObject);

                                        //arrayObject["filtered"] = true;
                                        //IncomingJson[tableNameJson][loopCount]["filtered"] = true;
                                    }

                                }
                                //loopCount++;
                            }
                        }
                        else
                        {
                            foreach (JArray arrayGroup in jsonRows) // The array group (e.g. fault_record_subform)
                            {
                                // We are looking through the json array, to find any rows that match our filter key and filter value.
                                // We will then add that into our jsonRows
                                //int loopCount = 0;

                                foreach (JObject arrayObject in arrayGroup) // Each group can have a set of arrays. (each  record has multiple sub records)
                                {

                                    foreach (var objectItem in arrayObject)
                                    {
                                        string objectItemValue = string.Empty;

                                        if (objectItem.Value.GetType() == typeof(JArray))
                                        {
                                            foreach (var item in objectItem.Value)
                                            {
                                                objectItemValue += item;
                                            }
                                        }
                                        else
                                        {
                                            objectItemValue = (string)objectItem.Value;
                                        }

                                        if (objectItem.Key == FilterField && objectItemValue == FilterValue)
                                        {
                                            // We need to save the item.
                                            filteredArray.Add(arrayObject);
                                            //testArray.Add(arrayObject);
                                            //arrayObject["filtered"] = true;
                                            //IncomingJson[tableNameJson][loopCount]["filtered"] = true;
                                        }

                                    }
                                }
                                //loopCount++;
                            }
                        }

                        //filteredArray.CopyTo(testArray, 0);

                        jsonRows = filteredArray;   // limit the jsonRows to the filtered set (overwrite the jsonRows)

                    }
                }

            }
            else
            {
                // This is not a filter set
                JArray singleArray = new JArray();

                foreach(var arraySet in jsonRows)
                {
                    //if (!testArray.Intersect(arraySet).Any())
                    {
                        if (arraySet.GetType() == typeof(JObject))
                        {
                            singleArray.Add(arraySet);
                        }
                        else
                        {
                            foreach (JObject arrayObject in arraySet)
                            {
                                singleArray.Add(arrayObject);
                            }
                        }
                    }
                }

                jsonRows = singleArray;
            }

        }

What I need ultimately (the jsonRows will be used elsewhere in my code within the loop) is that the third set will have items not found in the first 2 sets.



Solution 1:[1]

After a bit of further experimentation, using dotnetfiddle as introduced to me by @dbc (thank you), I have created a List and added each arrayObject into the list during the filtering stages.

I then during the unfiltered stage check if my arraySet is contained in the List, and if not, then add that item to the remaining jsonRows, thereby giving me the balance of the original list.

As can be seen here... https://dotnetfiddle.net/ot35Z2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David