score:0

Accepted answer
        var peoplemultigrouping = from p in people
                                  let ageselection =
                                        p.age < 20
                                        ? "young" 
                                        : p.age >= 20 && p.age <= 40
                                            ? "adult" 
                                            : "senior"
                                  group p by ageselection;

        foreach (var p in peoplemultigrouping)
        {
            console.writeline($"{p.key}");
            foreach (var i in p)
            {
                console.writeline($" {i.age}");
            }
        }

score:0

var multiplegrps = people.groupby(x=> x.age)
                         .todictionary(x=>x.key,
                                       x=>x.select(y=>new {  
                                                 younggroup=y.where(z=>z.age<= 20), 
                                                 adultgroup=y.where(z=>z.age >=21 && a.age <= 40), 
                                                seniorgroup=y.where(z=> z.age >=41) 
                                                          });

score:0

foreach (var item in multiplegrps)
{
       foreach (var person in item.younggroup.selectmany(x => x))
       {
            console.writeline($"name: {person.name}, age: {person.age}");
       }
}

score:0

var allgroupsinoneobject = new {
   youngpeople = classifiedpersons["young"],
   adultpeople = classifiedpersons["adult"],
   seniorpeople = classifiedpersons["senior"]
};

score:0

----------
foreach (var item in multiplegrps)
            {
                foreach (var yitem in item.younggroup)
                {
                    if (item.younggroup.count() > 0 && yitem.elementat(0) != null)
                    {
                        console.writeline("young : " + yitem.elementat(0).name);
                    }
                }
                foreach (var aitem in item.adultgroup)
                {
                    if (item.adultgroup.count() > 0 &&aitem.elementat(0) != null)
                    {
                        console.writeline("adult : " + aitem.elementat(0).name);
                    }
                }
                foreach (var sitem in item.seniorgroup)
                {
                    if (item.seniorgroup.count() > 0 &&sitem.elementat(0) != null)
                        {
                            console.writeline("senior : " + sitem.elementat(0).name);
                        }
                }
            }

score:1

var multiplegrps =
    from p in people
    let groupname = getgroupname(p.age)
    group p by groupname
    into groups
    group groups by groups.key;

foreach (var group in multiplegrps)
{
    foreach (var member in group.selectmany(i => i))
    {
        console.writeline($"{group.key}, {member.age}, {member.name}");
    }
}

private static string getgroupname(int age)
{
    return age <= 20
        ? "younggroup"
        : age <= 40
            ? "adultgroup"
            : "seniorgroup";
}

Related Query

More Query from same tag