0

I have few list List<Parent> where Parent class is

public class Parent
{
    public string RegionName { get; set; }
    public List<PhysicalRule> PhysicalRules { get; set; }
}

public class PhysicalRule
{
    public string Region { get; set; }
    public string LayerGroup { get; set; }
    public string NetClassGroup { get; set; }
    public List<RuleProperty> Properties { get; set; }
    public string RuleName { get; set; }

}
public class RuleProperty
{
    public string Name { get; set; }
    public object Value { get; set; }
}

I want to group the List<Parent> as

{
    NetClassGroup1
    [
        {
            Region: region1
            List<PhysicalRules>
        },
        {
            Region: region2
            List<PhysicalRules>
        },
        {
            Region: region3
            List<PhysicalRules>
        }
    ]

    NetClassGroup2
    [
        {
            Region: region1
            List<PhysicalRules>
        },
        {
            Region: region2
            List<PhysicalRules>
        },
        {
            Region: region3
            List<PhysicalRules>
        }
    ]
}

The order of Region objects should be the same in all the NetclassGroup Objects. I am planning to sort by Region name...but not sure how to construct this structure form the data objects that I have.

Programmerzzz
  • 1,237
  • 21
  • 48
  • 1
    Your question is really too broad. But, you can use LINQ's "group by" to group each list by `NetClassGroup`. See duplicates. You can use `SelectMany()` to flatten the original collection of lists into individual `PhysicalRule` objects before grouping, then after the grouping by `NetClassGroup`, perform another grouping within each of those groups to recover `Parent` objects for each of those groups. – Peter Duniho Jul 21 '21 at 23:03
  • 1
    ` var query = from parent in parentsList orderby parent.RegionName from phisicalRule in parent.PhysicalRules group parent by phisicalRule.NetClassGroup into grp select grp; var dic = query.ToDictionary(q=>q.Key); ` – AliReza Sabouri Jul 21 '21 at 23:15

0 Answers0