简体   繁体   English

加载WCF RIA服务查询结果到ObservableCollection

[英]Loading WCF RIA Services Query results to an ObservableCollection

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: 在我的Silverlight应用程序中,在创建ADO.NET实体数据模型和WCF RIA服务域服务类之后,在相应的ProductService类中,我有一个查询操作,它将一组Product实体返回给客户端,如下所示:

public IQueryable<Product> GetProducts()
{
   return this.ObjectContext.Products;
}

Now I'm trying to read it in the client Silverlight app and load results to an ObservableCollection: 现在我试图在客户端Silverlight应用程序中读取它并将结果加载到ObservableCollection:

ProductContext pcontext = new ProductContext();
ObservableCollection<Prod> prAvs = pcontext.GetProductsQuery();

But getting an error: 但是得到一个错误:

Cannot implicitly convert type System.ServiceModel.DomainServices.Client.EntityQuery<MyTestApp.Web.Product> to System.Collections.ObjectModel.ObservableCollection<MyTestApp.Prod> 无法将System.ServiceModel.DomainServices.Client.EntityQuery<MyTestApp.Web.Product>类型隐式转换为System.Collections.ObjectModel.ObservableCollection<MyTestApp.Prod>

How could I fix that issue? 我怎么能解决这个问题?

First problem: 第一个问题:

You should be using the client-side Products class generated for you by RIA services, not another class you define yourself. 您应该使用RIA服务为您生成的客户端Products类,而不是您自己定义的另一个类。

eg you should have a collection of MyTestApp.Web.Product, not of MyTestApp.Prod objects. 例如,您应该拥有MyTestApp.Web.Product的集合,而不是MyTestApp.Prod对象的集合。

You will find the generated domain context in a hidden Generated_Code folder in your client project. 您将在客户端项目的隐藏Generated_Code文件夹中找到生成的域上下文。 Within that will be a MyTestApp.Web.g.cs file containing the client side context and any data objects (like MyTestApp.Web.Product). 其中包含一个MyTestApp.Web.g.cs文件,其中包含客户端上下文和任何数据对象(如MyTestApp.Web.Product)。

Second issue: 第二期:

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

The result (when the load completes) is an entity collection in the returned loadOperation object. 结果(加载完成时)是返回的loadOperation对象中的实体集合。 You can use the entity collection immediately, but it is initially empty. 您可以立即使用实体集合,但它最初是空的。

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

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