I have two objects that are linked, States and Cities, so each State has his Cities and each Citie is linked to an State. I also have some Units that have stateID and citieID but they are not linked since i have them only in Json.
What i need is to get only the States and Cities that have Units. I managed to get the first two but was wondering if there was any faster way to do it since i will have to make an update on those datas everyday:
//unitsData have a List of Units objects, this only have stateID, citieID and the unit data var unitsData = objUnidade.BuscaUnidades(); //unitsState have all units grouped by State, here i also only have stateID and citieID, same data as above var unitsState = unitsData.GroupBy(x => x.codigoEstado); //Here is where i make my search inside the unidadesEstados and select only the Estados that i need var activeStates = unitsState.Select(est => db.States.FirstOrDefault(x => x.ID == est.Key)).Where(state => state != null).ToList(); To do the Cities search i'm doing the same but using an extra foreach, is there a way to make this better ?