Home > Software design >  ef core 5 Group By items in list
ef core 5 Group By items in list

Time:02-15

Hi I want to group a list by items inside an array

var localitiesList = localities.Where(x => x.Level == 1).Select(x=>x.Name);



 var doctorsGoupedByLocalities = docs.GroupBy(x => localitiesList).Select(doctor => new {
                    LocalityName = doctor.Key,
                    doctors = _mapperService.Map<IEnumerable<Doctor>, IEnumerable<DoctorDto>>(doctor.Select(x => x))
                });

the result by doing it this way

   { 
"localityName": [
                "item 1",
                "item2"
            ],
"doctors": [..]

}

but what I need as a response is something like this

   {[{ 
"localityName": [
                "item 1"  
            ],
"doctors": [..]
},   { 
"localityName": [
                "item 2"  
            ],
"doctors": [..]
}]

How can I get a response like that by grouping from items inside an array.

Any help is much appreciated. Thank you.

CodePudding user response:

you are grouping by a list base on another list, this is not proper way. you have to group by docs by a property of their own, if you haven't such property you have to create new relation between doctors and localities

  • Related