繁体   English   中英

无法将DataServiceOrderedQuery类型的对象强制转换为Microsoft.OData.Client.DataServiceCollection类型

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

我有一个名为Foos的数据库表,其中包含foo类型的记录。 我还有另一个名为Bars表,其中包含bar类型的记录。 我试图在数据库中查询集合foos并将该集合放置在bar类型的记录中。

像这样:( foobar都在第3方库中定义,我可以检查元数据,但不能更改它们)

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();
}

当我尝试将第二个查询的结果分配给bar.Foos我得到了错误

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

如何将Container.Foos.Where(x => x.someProperty == someRequirement)的结果分配给bar.Foos

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

应该更改为:

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

也可以在https://msdn.microsoft.com/zh-cn/library/ee652823(v=vs.110).aspx上进行小测验。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM