c# - write generic method for paging -


i have controllers paging calculated. have 13 different controllers. write calculation in every controller tedious.

this comoplete method:

 [route("sort/{sortcolumn}/{sortorder?}", name = "sort-product")]         [route("page/{page:int}/{sortcolumn}/{sortorder?}", name = "paging-product")]         [route("search/{searchstring}")]         [route("index")]         public actionresult index(string searchstring, string filter, string currentfilter, string sortcolumn, string sortorder, int? page)         {             iorderedqueryable<product> entities = (iorderedqueryable<product>)db.filteredproducts;              if (searchstring != null) page = 1; else searchstring = currentfilter;              if (filter != null) {                 string[] filters = filter.split(new char[] { '.' });                 filter = "";                  // filter on form                 if (filters.length > 0 && !string.isnullorempty(filters[0])) {                     formlibraryentry formentry = formlibrarycontroller.getformlibraryentry(filters[0], statehelper.getschema());                      if (formentry != null) {                         entities = (iorderedqueryable<product>)entities.where(                             s => s.formname == formentry.id                         );                         addfixednotification(string.format(resources.entity.environment.filteredbyformmessage, formentry.name));                         filter += filters[0];                     }                 }                  // filter on design template                 if (filters.length > 1 && !string.isnullorempty(filters[1])) {                     var designentry = designtemplatecontroller.gettemplatelibraryentry(filters[1], statehelper.getschema());                      if (designentry != null) {                         entities = (iorderedqueryable<product>)entities.where(                             s => s.templatename == designentry.id                         );                         addfixednotification(string.format(resources.entity.environment.filteredbydesigntemplatemessage, designentry.name));                         filter += "." + filters[1];                     }                  }             }              if (!string.isnullorempty(searchstring)) {                 entities = (iorderedqueryable<product>)entities.where(                         s => s.name.toupper().contains(searchstring.toupper())                         || (!string.isnullorempty(s.formname) && s.formname.toupper().contains(searchstring.toupper()))                         || (!string.isnullorempty(s.urlname) && s.urlname.toupper().contains(searchstring.toupper()))                 );                 addfixednotification(string.format(resources.entity.environment.filteredbysearchtermmessage, searchstring));             }              switch (sortcolumn) {                 case "id":                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.id) : entities.orderby(s => s.id);                     break;                 case "name":                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.name) : entities.orderby(s => s.name);                     break;                 case "enabled":                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.isenabled) : entities.orderby(s => s.isenabled);                     break;                 case "formname":                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.formname) : entities.orderby(s => s.formname);                     break;                 case "design":                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.templatename) : entities.orderby(s => s.templatename);                     break;                 case "urlname":                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.urlname) : entities.orderby(s => s.urlname);                     break;                 case "forms":                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.submittedforms.count()) : entities.orderby(s => s.submittedforms.count());                     break;                 case "modified":                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.modificationdate) : entities.orderby(s => s.modificationdate);                     break;                 default:                     sortcolumn = "name";                     sortorder = "";                     entities = (sortorder == "desc") ? entities.orderbydescending(s => s.name) : entities.orderby(s => s.name);                     break;             }              viewbag.sortcolumn = sortcolumn;             viewbag.sortorder = sortorder == "desc" ? "desc" : "";             viewbag.searchstring = searchstring;             viewbag.filter = filter;              int pagesize = statehelper.getpagesize();             int pagenumber = statehelper.haspagesizechanged ? 1 : (page ?? 1);               object selectionproduct = modelhelper.getselectedmodelid("product");             if (selectionproduct != null) {                 ienumerable<ienumerable<product>> pp = entities.partition(pagesize);                 int calculatedpage = 0;                 bool found = false;                 foreach (var item in pp) {                     calculatedpage++;                     ienumerable<product> inner = item ienumerable<product>;                     foreach (var product in inner) {                         if (product.id == (int)selectionproduct) {                             found = true;                             viewbag.selectedrowproduct = product.id;                             break;                         }                     }                     if (found)                         break;                 }                 if (found)                     pagenumber = calculatedpage;              }              return view(entities.topagedlist(pagenumber, pagesize));         } 

but part of calculation:

object selectionproduct = modelhelper.getselectedmodelid("product");             if (selectionproduct != null) {                 ienumerable<ienumerable<product>> pp = entities.partition(pagesize);                 int calculatedpage = 0;                 bool found = false;                 foreach (var item in pp) {                     calculatedpage++;                     ienumerable<product> inner = item ienumerable<product>;                     foreach (var product in inner) {                         if (product.id == (int)selectionproduct) {                             found = true;                             viewbag.selectedrowproduct = product.id;                             break;                         }                     }                     if (found)                         break;                 }                 if (found)                     pagenumber = calculatedpage;              } 

so try build helper method this:

 public static bool findpage(type t, object modelid, ienumerable<type> entities, int pagesize, int calculatedpage, int? id)         {             if (modelid != null) {                 calculatedpage = 0;                 ienumerable<ienumerable<t>> pp = entities.partition(pagesize);                 int page = 0;                 bool found = false;                 foreach (var item in pp) {                     page++;                     ienumerable<type> inner = item ienumerable<type>;                     foreach (var product in inner) {                         if (id == (int)modelid) {                             found = true;                             break;                         }                     }                     if (found)                         break;                 }                 if (found)                     calculatedpage = page;                 else                     calculatedpage = 0;                  return found;             }             return false;         } 

but error:

the type or namespace name 't' not found (are missing using directive or assembly reference?)  

thank you

try:

public static bool findpage<t>(object modelid, ienumerable<t> entities, int pagesize, int calculatedpage, int? id)     {         if (modelid != null) {             calculatedpage = 0;             ienumerable<ienumerable<t>> pp = entities.partition(pagesize);             int page = 0;             bool found = false;             foreach (var item in pp) {                 page++;                 ienumerable<t> inner = item ienumerable<t>;                 foreach (var product in inner) {                     if (id == (int)modelid) {                         found = true;                         break;                     }                 }                 if (found)                     break;             }             if (found)                 calculatedpage = page;             else                 calculatedpage = 0;              return found;         }         return false;     } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -