使用Firebase数据库数据替换列表中的项目。

huangapple go评论57阅读模式
英文:

Replace items in list for items from FirebaseDatabase data

问题

我可以帮你翻译以下代码部分:

原文:

var GetItems = (await fc
            .Child("Verhaal")
            .OnceAsync<StudentModel>()).Select(item => new StudentModel

{            
Adres = item.Object.Adres,
Plaats = item.Object.Plaats
}).ToList(); // had to add this .ToList() to make the answer from Jessie to work.

 // Change it in to this 

StudentModel obj = new StudentModel();
obj.Adres = "test@gmail.com";
obj.Plaats = "Test1";
_allStudentList.Add(obj);

翻译后:

var GetItems = (await fc
            .Child("Verhaal")
            .OnceAsync<StudentModel>()).Select(item => new StudentModel
{
    Adres = item.Object.Adres,
    Plaats = item.Object.Plaats
}).ToList(); // 不得不添加 .ToList() 以使 Jessie 的答案有效。

// 更改为以下内容

StudentModel obj = new StudentModel();
obj.Adres = "test@gmail.com";
obj.Plaats = "Test1";
_allStudentList.Add(obj);
英文:

Found a project that loads items in a CollectionView from a ViewModel for InfiniteScrolling.
But i want to replace the items with data from a FirebaseDatabase.

How do i make this work ?

There are 25 items in the ViewModel in AddStudentInfo()

    private async void AddStudentInfo()
    {

        StudentModel obj = new StudentModel();
        obj.Plaats = &quot;Test1&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test2&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test3&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test4&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test5&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test6&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test7&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test8&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test9&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test10&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test11&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test12&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test13&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test14&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test15&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test16&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test17&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test18&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test19&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test20&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test21&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test22&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test23&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test24&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);

        obj = new StudentModel();
        obj.Plaats = &quot;Test25&quot;;
        obj.Adres = &quot;test@gmail.com&quot;;
        _allStudentList.Add(obj);
  
    }

I can load data with this from a FirebaseDatabase.

  var GetItems = (await fc
          .Child(&quot;Verhaal&quot;)
          .OnceAsync&lt;StudentModel&gt;()).Select(item =&gt; new StudentModel
          {
              Adres = item.Object.Adres,
              Plaats = item.Object.Plaats
          }).ToList(); // had to add this .ToList() to make the answer from Jessie to work.

What do i have to change to make this work. Items from Adres to obj.Adres etc.

 var GetItems = (await fc
        .Child(&quot;Verhaal&quot;)
        .OnceAsync&lt;StudentModel&gt;()).Select(item =&gt; new StudentModel

        {            
        Adres = item.Object.Adres,
        Plaats = item.Object.Plaats
        }).ToList(); // had to add this .ToList() to make the answer from Jessie to work.

         // Change it in to this 

        StudentModel obj = new StudentModel();
        obj.Adres = &quot;test@gmail.com&quot;;
        obj.Plaats = &quot;Test1&quot;;
        _allStudentList.Add(obj);

答案1

得分: 1

你可以尝试使用async-await来实现这个。

请参考以下的代码:

public ObservableCollection<StudentModel> AllStudentList { get; set; }

AllStudentList = new ObservableCollection<StudentModel>();

private async void LoadData()
{
    AllStudentList = new ObservableCollection<StudentModel>();
    
    //GetItems 应该是一个列表
    var GetItems = (await fc
        .Child("Verhaal")
        .OnceAsync<StudentModel>()).Select(item => new StudentModel
        {
            Adres = item.Object.Adres,
            Plaats = item.Object.Plaats
        });

    foreach (var item in GetItems) {
        AllStudentList.Add(item);
    }
}

希望对你有帮助。

英文:

You can try to use async-await to achieve this.

Please refer to the following code:

   public ObservableCollection&lt;StudentModel&gt; AllStudentList { get; set; }

    AllStudentList = new ObservableCollection&lt;StudentModel&gt;();


    private async  void LoadData()
    {

        AllStudentList = new ObservableCollection&lt;StudentModel&gt;();
        
        //GetItems should be a list
        var GetItems = (await fc
    .Child(&quot;Verhaal&quot;)
    .OnceAsync&lt;StudentModel&gt;()).Select(item =&gt; new StudentModel

    {
        Adres = item.Object.Adres,
        Plaats = item.Object.Plaats
    });

        foreach (var item in GetItems) {

            AllStudentList.Add(item);

        }
    }

huangapple
  • 本文由 发表于 2023年3月7日 19:37:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定