如何将对象列表保存到Firestore

时间:2020-08-31 05:52:22

标签: c# google-cloud-firestore

我需要将对象列表保存到Firestore数据库。下面是我的代码:

我如何创建对象列表

private async void LoadPlazaListAndSummary()
        {
            _plazaList = await _dc.GetPlazas();
            foreach(var item in _plazaList)
            {
                var par = new ParameterManualTags
                {
                    Plaza = item.Code,
                    Lane = item.Lane,
                    DateTo = System.DateTime.Now,
                    DateFrom = System.DateTime.Today
                };

                var manualCount = await _dc.GetManualAndTransactionCount(par);

                var model = new Summary
                {
                    Name = item.Name,
                    Lane = item.Lane,
                    Code = item.Code,
                    DateRun = System.DateTime.Now,
                    ManualAverage = 100.00,
                    ManualCount = manualCount[0],
                    ReadCount = 1,
                    TotalTransaction = manualCount[1],
                    Type = item.Type,
                    Plaza = item,
                    DateFrom = par.DateFrom,
                    DateTo = par.DateTo,
                    RfidAddress = item.ReaderIpAddress,
                    IsDedicated = item.IsDedicated ? "Dedicated" : "Mixed"
                };
                model.ManualAverage = Math.Round(ComputeManualPercentage(model), 2);
                _summaryList.Add(model);

                DgSummary.Items.Add(model);
            }
        }

这是将数据保存到Firestore的示例:

public void SetData()
        {
            if (_isConnected)
            {
                try
                {
                    var doc = _database.Collection("MyCollection").Document("myDocument");
                    Dictionary<string, object> data = new Dictionary<string, object>()
                    {
                        {"FirstName", "MyFirstName"},
                        {"LastName", "MyLastName"},
                        {"PhoneNumber", "PhoneNumber"}
                    };
                    var x = doc.SetAsync(data);
                    Console.WriteLine("Saved");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($@"An error occured in saving the data: {ex.Message}");
                }
            }
        }

使用示例代码,我可以一次在Firestore中插入一个,但是,我不知道如何将对象列表保存到Firestore。

0 个答案:

没有答案