问题
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