Darcy
Darcy

Reputation: 5368

Proper way to return IQueryable using WCF Data Service and EF

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.

Upvotes: 1

Views: 2369

Answers (1)

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13320

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.

Upvotes: 1

Related Questions