I am even not sure, that the question is asked correctly, because I have no idea how to name it. Sorry if confusing, but I will try to explain what I dont know step by step. In my VM one of the methods is calling a service method:
public ObservableCollection<LoadedJockey> Jockeys { get; private set; }
Jockeys = await _updateDataService.UpdateDataAsync(Jockeys, JPlFrom, JPlTo, "updateJockeysPl");
The service method takes argument of type ObservableCollection<T>
, so instead of Jockeys
can be Horses
, Cars
, Poneys
etc. And inside of my service method I need to assign to ObservableCollection<LoadedJockeys>
(known type) that generic collection passed to the parameter, to get something like this:
public async Task<ObservableCollection<T>> UpdateDataAsync<T>(ObservableCollection<T> genericCollection, int idFrom, int idTo, string jobType)
if (jobType.Contains("Horses"))
{
ObservableCollection<LoadedHorses> horses = genericCollection;
}
else if (jobType.Contains("Jockeys"))
{
ObservableCollection<LoadedJockeys> jockeys = genericCollection;
}
How to do it?
Cast the elements
ObservableCollection<LoadedJockeys> jockeys =
new ObservableCollection<LoadedJockeys>(genericCollection.Cast<LoadedJockeys>());
or take only the matching elements
ObservableCollection<LoadedJockeys> jockeys =
new ObservableCollection<LoadedJockeys>(genericCollection.OfType<LoadedJockeys>());