0

I am using Xamarin.Forms MVVM and sqlLite-net to make a Shared Mobile App.

I am getting an error bc 'AddRange' doesn't exist in ObservableCollection or I am using that function wrong.

All I want to do is to fill my ObservableCollection list. do I have to manually Add using loop? its weird ObservableCollection has a 'Add' method but no way to fill

Error: 'ObservableCollection' does not contain a definition for 'AddRange' and no accessible extension method 'AddRange' accepting a first argument of type 'ObservableCollection' could be found (are you missing a using directive or an assembly reference?)

using following refs:

using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 using System.Threading.Tasks;
using TestApp01_MVVM_Basic.Models;
using Xamarin.CommunityToolkit.ObjectModel;
 using Xamarin.Forms;
 using System.Collections.Specialized;
using TestApp01_MVVM_Basic.Services;
using System.Collections.Generic;
using System.Linq;

Below I am creating my ObservableCollection. I am getting my error on line: AddRange()

public ObservableCollection<ProductModel> ProductsList { get; set; }
public ProductViewModel()
    {
        ProductsList = new ObservableCollection<ProductModel>();
    }

 public async Task MyDisplayList()
    {
        var getData = ProductService.DisplayProduct();
        ProductsList.AddRange(getData);

    }

In Servies Class, I have following DisplayProduct() method. that returns a list from database

public static async Task<IEnumerable<ProductModel>> DisplayProduct()
    {
        await Init();

        var GetProduct = await db.Table<ProductModel>().ToListAsync();
        return GetProduct;
    }
  • 1
    `System.Collections.ObjectModel.ObservableCollection` does not have an `AddRange`method. You can use `InsertItem` instead. Here are the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.observablecollection-1?view=netstandard-1.2) – G Wimpassinger May 06 '21 at 22:54
  • https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each – zia khan May 06 '21 at 22:56

1 Answers1

0

What you are looking for is implemented on Xamarin community toolkit package: ObservableRangeCollection which sub-class System.Collections.ObjectModel.ObservableCollection<T>, you can use ObservableRangeCollection<ProductModel> instead of ObservableCollection<ProductModel> since the latter does not implement such method.

public ObservableRangeCollection<ProductModel> ProductsList { get; set; }
public ProductViewModel()
    {
        ProductsList = new ObservableRangeCollection<ProductModel>();
    }

 public async Task MyDisplayList()
    {
        var getData = await ProductService.DisplayProduct();
        ProductsList.AddRange(getData);

    }

public static async Task<IEnumerable<ProductModel>> DisplayProduct()
    {
        await Init();

        var GetProduct = await db.Table<ProductModel>().ToListAsync();
return await Task.FromResult(GetProduct);
    }
Cfun
  • 8,442
  • 4
  • 30
  • 62
  • thanks that makes sense now. Question I am getting error on line: ProductsList.AddRange(getData);. regarding cannot convert from System.Threading.Tasks.Task to system.collection.generice.IEumerable –  May 06 '21 at 23:08
  • `DisplayProduct` is an async method, so you need to use `await` when calling it – Jason May 06 '21 at 23:09
  • Thanks for pointing that out @Jason I didn't noticed it – Cfun May 06 '21 at 23:15
  • @ikhlasahmed mark the answer above as the answer if this is the actual answer to your question – riffnl May 06 '21 at 23:16
  • @ikhlasahmed getData needs to implement `IEnumerable` look at the definition of [AddRange(...)](https://github.com/xamarin/XamarinCommunityToolkit/blob/aba6205274a7fb8c8a55eaedb8001e3a693ed5f1/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/ObservableRangeCollection.shared.cs#L38) – Cfun May 06 '21 at 23:18
  • nice, you have probably used `return await Task.FromResult(GetProduct);` in `DisplayProduct()` – Cfun May 06 '21 at 23:25