逐一读取IEnumerable List <>

问题描述

基本上,我需要读取具有两种类型的值的列表,一种是ID整数,另一种是字符串。

像这样加载:

private async Task<List<Pisteros>> GetTasksAsync()
        {
            List<Pisteros> PisterosLista = new List<Pisteros>();
            HttpClient client = new HttpClient();
            Uri uri = new Uri(string.Format("https://servicentroapi.azurewebsites.net/api/Pisteros",string.Empty));
            HttpResponseMessage response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();
                PisterosLista = JsonConvert.DeserializeObject<List<Pisteros>>(content);
                Console.WriteLine("content :: " + content);
                Console.WriteLine("Data :: " + PisterosLista);
            }
            return PisterosLista;
        }

它被调用并直接加载到选择器中,因此我需要一口气读取List或选择器以将数据插入其他表中

这是电话:

 pck_Pisteros.ItemsSource = await GetTasksAsync();
                List<Pisteros> PisterosLista = new List<Pisteros>();
                PisterosLista = await GetTasksAsync();

                //update Local DB
                UpdatePisterosLocal(PisterosLista);

无论它更容易阅读,目前我都有这两个“选项”

private async void UpdatePisterosLocal(List<Pisteros> PisterosLista)
        {
            try
            {
                PisterosDBController pistDB = new PisterosDBController();
                Pisteros_Local pistLocal = new Pisteros_Local();

                //option 1 read the list
                foreach (string element in PisterosLista)
                {
                    pistLocal.IDPistero = ???
                    pistLocal.PisteroN = ????
                }

                //option 2 read the picker
                int total = pck_Pisteros.Items.Count();
                for (int i = 0; i < total; i++)
                {
                    pck_Pisteros.SelectedIndex = i;
                    pistLocal.IDPistero = ???
                    var result = ???
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

我想我希望阅读选择器,以避免两次调用GetTasksAsync(),这可能会使我猜到的应用程序变慢

解决方法

这是我最终做到的方式

foreach (var item in PisterosLista)
        {
            var DatosRegistro = new T_Pisteros
            {
                PisteroID = item.PisteroID,PisteroN = item.PisteroN
            };
            var num = db.Insert(DatosRegistro);
        }

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...