使用WCF数据服务和EF返回IQueryable的正确方法 [英] Proper way to return IQueryable using WCF Data Service and EF

查看:228
本文介绍了使用WCF数据服务和EF返回IQueryable的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想返回一些数据,我正在使用WCF数据服务和实体框架,如下所示:

So I want to return some data, and I'm using WCF Data Services and Entity Framework, that looks like:

public class MyWcfDataService : DataService<MyEFModel>
{
   [WebGet(ResponseFormat = WebMessageFormat.Json)]
   public IQueryable<GetMyListEF> GetMyList()
   {
       using (MyEfModel context = this.CurrentDataSource)
       {
           return context.GetMyListEF().ToList().AsQueryable();
       }
   }
}

正如你所见, m投射到列表,然后查询。如果我只投递 AsQueryable(),我将无法读取数据,因为连接已关闭(由于延迟执行AsQueryable)。

As you can see I'm casting to a list, then to queryable. If I only cast AsQueryable(), I won't be able to Read the data because the connection has closed (due to deferred execution of AsQueryable).

所以我的问题是,有没有更好的方法?是否需要使用语句的?数据有时可能是100k行,所以转换到列表使用大量的内存。真正利用延期执行也只能返回一个真正的IQueryable也是很好的。

So my question is, is there a better way? Is the using statement even needed? The data can sometimes be 100k rows, so casting to a list uses a fair amount of memory. It would also be nice to really take advantage of deferred execution and only return a true IQueryable.

推荐答案

你不需要实际上最好不要使用它。 WCF数据服务将在请求结束时处理CurrentDataSource。所以只是用它。

You don't need the using, in fact it's better not to have it. The WCF Data Service will dispose the CurrentDataSource at the end of the request. So just use it.

这篇关于使用WCF数据服务和EF返回IQueryable的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆