0
  [ICommand]
    async Task GetStatesAsync(string partname)
    {
        try
        { 
            var states = await cityService.GetStates(partname);   

            StatesList.Clear();


            foreach (var state in states)
            {
                var citylist = await cityService.GetCities(state.Id); 
                state.CityList = citylist;
                state.CityCount = citylist.Count;
                StatesList.Add(state);
            } 
        }
        catch (Exception ex)
        {
            await App.Current.MainPage.DisplayAlert("Message", ex.Message, "OK");
        }
        finally
        {

        }
    }

I mean this part

var citylist = await cityService.GetCities(state.Id); 

of the code runs once, i.e. for the first time and all states get cities of the first record. What is wrong whith this?

Shaahin
  • 1,195
  • 3
  • 14
  • 22
  • from a url I mean an online webApi – Shaahin Aug 01 '22 at 05:52
  • What does `GetCities` return? IEnumerable/IQueryable? You might use the `ToList(..)` to persist it. – Jeroen van Langen Aug 01 '22 at 06:40
  • As far as I can see there is nothing obvious wrong with the example code. What happens if you run the method a second time? How does the WebAPI code look? Does the `cityService` do anything more than creating the URI and parsing the result? – JonasH Aug 01 '22 at 06:45
  • Yes, GetCities return a List. – Shaahin Aug 01 '22 at 06:48
  • The only thing I can see that, Í run the code in development(emulator), probobly because of netowork connection, cannot run tow async methods in the same call. – Shaahin Aug 01 '22 at 06:50
  • just try with for loop instead of foreach – Shakir Ahamed Aug 01 '22 at 07:13
  • You can try to use the `List()` and the `Task.WhenAll()` to do that. Such as [the case](https://stackoverflow.com/questions/52082617/c-sharp-await-in-for-loop) and [this one](https://stackoverflow.com/questions/19431494/how-to-use-await-in-a-loop). @Shaahin – Liyun Zhang - MSFT Aug 02 '22 at 03:49

0 Answers0