I have a data structure in my app that looks a bit like this (this is just an small example).
public class Category
{
public string Name { get; set; }
public List<SubCategory> subCategories { get; set; }
}
public class SubCategory
{
public int order { get; set; }
public string Name { get; set; }
}
And with this structure I create a list like this:
class Program
{
static void Main(string[] args)
{
var duplicateCategory = new Category
{
Name = "E-platform",
subCategories = new List<SubCategory> {
new SubCategory
{
Name = "App",
order = 1
},
new SubCategory
{
Name = "Layout",
order = 2
},
new SubCategory
{
Name = "Sidepanel",
order = 3
}
}
};
var duplicateCategory2 = new Category
{
Name = "E-platform",
subCategories = new List<SubCategory> {
new SubCategory
{
Name = "Website",
order = 1
},
new SubCategory
{
Name = "Layout",
order = 2
}
}
};
var categories = new List<Category>
{
//APP
duplicateCategory,
duplicateCategory,
new Category
{
Name = "E-platform",
subCategories = new List<SubCategory> {
new SubCategory
{
Name = "App",
order = 1
},
new SubCategory
{
Name = "Layout",
order = 2
}
}
},
//WEBSITE
duplicateCategory2,
duplicateCategory2,
new Category
{
Name = "E-platform",
subCategories = new List<SubCategory> {
new SubCategory
{
Name = "Website",
order = 1
},
new SubCategory
{
Name = "Layout",
order = 2
},
new SubCategory
{
Name = "Sidepanel",
order = 3
}
}
}
};
}
}
Now from this list I need to remove the duplicates, this I have already achieved using "Distinct" of linq.
An extra requirement however is that I need to get the longest chain of the same subcategories out of this list. So given the list above, I would like to ONLY return these 2 objects:
new Category
{
Name = "E-platform",
subCategories = new List<SubCategory> {
new SubCategory
{
Name = "App",
order = 1
},
new SubCategory
{
Name = "Layout",
order = 2
},
new SubCategory
{
Name = "Sidepanel",
order = 3
}
}
};
And
new Category
{
Name = "E-platform",
subCategories = new List<SubCategory> {
new SubCategory
{
Name = "Website",
order = 1
},
new SubCategory
{
Name = "Layout",
order = 2
},
new SubCategory
{
Name = "Sidepanel",
order = 3
}
}
}
So as you can see it removed 1 of "E-platform -> 1. App -> 2. layout -> 3.Sidepanel" from the list to avoid duplicates (this I already figured out).
And it also removed "E-platform -> 1. App -> 2. layout" because "E-platform -> 1. App -> 2. layout -> 3.Sidepanel" has one extra level (this I can't figure out)
Do keep in mind that it is possible to be more than one level deeper, I just want the deepest category without throwing away other distinct categories.
Any help in getting this list would be very helpful! Let me know if you need more information.