Cannot implicitly convert type System.Collections.ObjectModel.ObservableCollection<> to System.Collections.Generic.List<>

I've got WCF service which contains List with data from LibW.dll(my dll). In main program I also have got list from LibW.dll.

I return list from WCF service

[OperationContract]
public List<IWeather> Final()
{
    return returner;
}

and then try to set the result of method to value

cont = e.Result;

where

List<LibW.IWeather> cont=new List<LibW.IWeather>();

But I've got such error Cannot implicitly convert type System.Collections.ObjectModel.ObservableCollection<object>' to 'System.Collections.Generic.List<NavigationGadget.IWeather>

What's wrong?

Upvotes: 1

Views: 16999

Answers (2)

AaronBastian
AaronBastian

Reputation: 307

One thing to consider is that in your service reference's configuration (right click on the service reference, then click "Configure Service Reference"), changing the collection type from "Observable Collection" to "System.Collections.Generic.List"

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500805

Presumably e.Result is an ObservableCollection<T> then... even if you've declared it as a List<IWeather> in your service.

It looks like you also need to cast from object to IWeather - assuming each result really is an IWeather. You could always copy it to a list, like this:

cont = e.Result.Cast<IWeather>().ToList();

... or change your variable type so it can handle any IList<IWeather>.

Upvotes: 6

Related Questions