Proper way to return IQueryable using WCF Data Service and EF

ε祈祈猫儿з 提交于 2019-12-11 09:04:27

问题


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

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).

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.


回答1:


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.



来源:https://stackoverflow.com/questions/7190956/proper-way-to-return-iqueryable-using-wcf-data-service-and-ef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!