c# - How to return IQueryable<T> for further querying -
i'm trying pagedlist.mvc library here
https://github.com/troygoode/pagedlist
which has usage sample
var products = myproductdatasource.findallproducts(); //returns iqueryable<product> representing unknown number of products. thousand maybe? var pagenumber = page ?? 1; // if no page specified in querystring, default first page (1) var onepageofproducts = products.topagedlist(pagenumber, 25); // contain 25 products max because of pagesize
typical implmentations of myproductdatasource.findallproducts(); along lines of
public iquerable<t> myproductdatasource.findallproducts() { using ( var ctx = new myctx() ) { return ctx.mylist().where( .... ); } }
which of course has invalidoperationexception() , dbcontext disposed message
looking best practices on how return iqueryable can used here without issues ?
you need "move up" scope of data context:
public iquerable<t> myproductdatasource.findallproducts(myctx context) { return context.mylist().where( .... ); }
then create context @ larger scope:
using (var ctx = new myctx()) { var products = myproductdatasource.findallproducts(ctx); var pagenumber = page ?? 1; var onepageofproducts = products.topagedlist(pagenumber, 25); }
Comments
Post a Comment