Reputation: 1310
I'm trying to convert my class AllStock
to ObservableCollection<AllStock> stockList
but I get the following error:
The best overloaded method match for 'System.Collections.ObjectModel.Collection.Add(Haze.AllStock)' has some invalid arguments
Here is my coding:
I create my Observable Collection -
private ObservableCollection<AllStock> _stockList;
public ObservableCollection<AllStock> stockList
{
get
{
if (_stockList == null)
_stockList = new ObservableCollection<AllStock>();
return _stockList;
}
}
Inside my method -
var allStock = await service.GetSysproStockAsync();
var stock = allStock.Select(x =>
new AllStock
{
Id = x.Id,
...
MaterialThickness = x.MaterialThickness
});
stockList.Add(stock); //Error here - I want to add my 'var stock' to my ObservableCollection
dgSysproStock.ItemsSource = stockList;
Why would it be throwing out this error?
Upvotes: 2
Views: 733
Reputation: 2670
The Add() for ObservableCollection requires a List so you need to provide a List instead.
Upvotes: 1
Reputation: 13394
Your Linq's Select method returns an IEnumerable but your stockList.Add has no overload for this datatype (only AllStock).
foreach(AllStock aStock in stock)
stockList.Add(aStock)
should work. The ObservableCollection unfortunatley doesn't have a AddRange method like the List<>
Upvotes: 5