装载WCF RIA服务的查询结果,到一个ObservableCollection [英] Loading WCF RIA Services Query results to an ObservableCollection
问题描述
在我的Silverlight应用程序,创建ADO.NET实体数据模型和WCF RIA服务域服务类后,在相应的ProductService I类有一个返回实体产品到客户端的集合,如下查询操作:
In my Silverlight app, after creating ADO.NET Entity Data Model and WCF RIA Services Domain Service Class, in a corresponding ProductService class I have a query operation that returns a collection of Product entities to the client, as follows:
public IQueryable<Product> GetProducts()
{
return this.ObjectContext.Products;
}
现在我想在客户端Silverlight应用程序和负载结果来阅读到一个ObservableCollection:
Now I'm trying to read it in the client Silverlight app and load results to an ObservableCollection:
ProductContext pcontext = new ProductContext();
ObservableCollection<Prod> prAvs = pcontext.GetProductsQuery();
但得到一个错误:
But getting an error:
无法隐式转换类型 System.ServiceModel.DomainServices.Client.EntityQuery< MyTestApp.Web.Product>
到 System.Collections.ObjectModel.ObservableCollection< ; MyTestApp.Prod>
我怎么能解决这个问题。
How could I fix that issue?
推荐答案
您应该使用客户端产品
类通过RIA服务,而不是自己定义的另一个类为您生成。
First problem:
You should be using the client-side Products
class generated for you by RIA services, not another class you define yourself.
例如,你应该有MyTestApp.Web.Product的集合,而不是MyTestApp.Prod对象。
e.g you should have a collection of MyTestApp.Web.Product, not of MyTestApp.Prod objects.
你会发现在你的客户端项目在一个隐藏文件夹Generated_Code生成域上下文。 。在这将是包含客户端环境和任何数据对象(如MyTestApp.Web.Product)一个MyTestApp.Web.g.cs文件
You will find the generated domain context in a hidden Generated_Code folder in your client project. Within that will be a MyTestApp.Web.g.cs file containing the client side context and any data objects (like MyTestApp.Web.Product).
您不能只投一个查询到一个集合。
You can't just cast a query to a collection.
您需要使用查询来加载。实体变更集而不是
You need to use the query to load an entity change-set instead.
var loadOperation = pcontext.Load(pcontext.GetProductsQuery());
结果(加载完成时)在返回loadOperation对象实体的集合。您可以立即使用实体集合,但它最初是空的。
The result (when the load completes) is an entity collection in the returned loadOperation object. You can use the entity collection immediately, but it is initially empty.
这篇关于装载WCF RIA服务的查询结果,到一个ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!