davecove
davecove

Reputation: 1071

Unable to cast object of type DataServiceOrderedQuery to type Microsoft.OData.Client.DataServiceCollection

I have a database table called Foos that contains records of type foo. I have another table called Bars that contains records of type bar. I am trying to query the database for a collection foos and place that collection in a record of type bar.

Something like this: (both foo and bar are defined in a 3rd party library, I can examine the metadata, but I can't change them)

public class bar (from metadata)
{
    public DataServiceCollection<foo> Foos { get; set; }
}

private void SaveSomeFoos()
{        
    var bar = Container.Bars.Where(x => x.someProperty == someRequirement).First();
    bar.Foos = Container.Foos.Where(x => x.someProperty == someRequirement);

    Container.SaveChanges();
}

When I try and assign the results of the 2nd query to bar.Foos I get the error

Unable to cast object of type DataServiceOrderedQuery to type Microsoft.OData.Client.DataServiceCollection

How do I assign the results of Container.Foos.Where(x => x.someProperty == someRequirement) to bar.Foos?

Upvotes: 0

Views: 1332

Answers (1)

mjwills
mjwills

Reputation: 23898

bar.Foos = Container.Foos.Where(x => x.someProperty == someRequirement);

should be changed to:

bar.Foos = new DataServiceCollection<Foo>(Container.Foos.Where(x => x.someProperty == someRequirement));

Also have a squiz at https://msdn.microsoft.com/en-us/library/ee652823(v=vs.110).aspx .

Upvotes: 1

Related Questions