C
C#3y 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;
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))
if(!DoesIdExistInHigherHierachyModel(int.Parse(highId), ids.Id) && !idsToRemove.Contains(ids.Id))
Doesnt give the same result Thanks in advance
15 Replies
dancepanda42
dancepanda423y ago
allIds.Select(x => x.Id).Intersect(higherHierachyIds.Select(x => int.Parse(x));
allIds.Select(x => x.Id).Intersect(higherHierachyIds.Select(x => int.Parse(x));
that should work (not tested)
Buck3tt
Buck3ttOP3y ago
instead of all loops?
ero
ero3y ago
i don't even know what this does lol what's DoesIdExistInHigherHierachyModel
Buck3tt
Buck3ttOP3y ago
Basicly just returns true or false
dancepanda42
dancepanda423y ago
Oh at the end it needs a. Any()
Buck3tt
Buck3ttOP3y ago
If there is a relation between an id and something in the higher stuff
dancepanda42
dancepanda423y ago
This linq query get at first All ids from the one list and intersect them with the ids from the other list
dancepanda42
dancepanda423y ago
If there is any entry in both list Any() return true
Buck3tt
Buck3ttOP3y ago
I might explain this wrong Or do u mean to replace all of the code with the linq or just a part of the code?
dancepanda42
dancepanda423y ago
All Code 😉
Buck3tt
Buck3ttOP3y ago
Since the string list itself wont have any similarity to the allids one So it will never match must be after i assume
dancepanda42
dancepanda423y ago
Ohh I missed the one DoesItExists.... method
Buck3tt
Buck3ttOP3y ago
No worries This will have to work for now lol
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?