C#C
C#4y ago
Buck3tt

❔ Somewhat advanced check for ids in foreach, remove duplicated

Hi!
Right now im doing somewhat of a "complex" (at least for me to understand) check of id's to remove, and wanna ask if i can do this code example simpler, or shorter, cuz it feels like i could, but cant think of how

private List<int> CheckIfIdsIsInHigher
Hierarchy(List<LowerModel> allIds, List<string> higherHierachyIds)

var idsToRemove = new List<int>();
var idsToNotRemove = new List<int>();

foreach (var highIds in higherHierachyIds)
            {
                foreach (var ids in allIds)
                {
                    if (!DoesIdExistInHigherHierachyModel(int.Parse(highId), ids.Id))
                    {
                        if (!idsToRemove.Contains(ids.Id))
                        {
                            idsToRemove.Add(ids.Id);
                        }
                    }
                    else
                    {
                        idsToNotRemove.Add(ids.Id);
                    }
                }
            }
            var result = idsToRemove.Except(idsToNotRemove).ToList();
            return result;


Since i tried to combine
if(!DoesIdExistInHigherHierachyModel(int.Parse(highId), ids.Id) && !idsToRemove.Contains(ids.Id))
Doesnt give the same result

Thanks in advance
Was this page helpful?