c# - Declaring 2 similar anonymous objects -
below declaration of 2 anonymous objects. second 1 exact same copy of first 1 except there no lastrefresh
element inside it.
is there way declare these 2 objects without duplicating exact same portion?
var routevaluesforautorefresh = new { page = @viewbag.nextpage, lastrefresh = @viewbag.lastrefresh, searchterm = request["searchterm"], searchcolumn = request["searchcolumn"], searchorder = request["searchorder"], searchdescending = (request["searchdescending"] ?? "true").tolower().indexof("true") > -1, requeststatus0 = (request["requeststatus0"] ?? "false").tolower().indexof("true") > -1, requeststatus1 = (request["requeststatus1"] ?? "true").tolower().indexof("true") > -1, requeststatus3 = (request["requeststatus3"] ?? "true").tolower().indexof("true") > -1, requeststatus5 = (request["requeststatus5"] ?? "true").tolower().indexof("true") > -1, requeststatus7 = (request["requeststatus7"] ?? "true").tolower().indexof("true") > -1, requeststatus9 = (request["requeststatus9"] ?? "true").tolower().indexof("true") > -1 }; var routevaluesfornextpage = new { page = @viewbag.nextpage, searchterm = request["searchterm"], searchcolumn = request["searchcolumn"], searchorder = request["searchorder"], searchdescending = (request["searchdescending"] ?? "true").tolower().indexof("true") > -1, requeststatus0 = (request["requeststatus0"] ?? "false").tolower().indexof("true") > -1, requeststatus1 = (request["requeststatus1"] ?? "true").tolower().indexof("true") > -1, requeststatus3 = (request["requeststatus3"] ?? "true").tolower().indexof("true") > -1, requeststatus5 = (request["requeststatus5"] ?? "true").tolower().indexof("true") > -1, requeststatus7 = (request["requeststatus7"] ?? "true").tolower().indexof("true") > -1, requeststatus9 = (request["requeststatus9"] ?? "true").tolower().indexof("true") > -1 };
there no syntax it, have couple of decent workarounds:
- make class named, , derive 1 other (or derive both common base)
- make third anonymous object common fields, use in both "real" named objects
- make individual local variables, , use them make fields of both anonymous objects.
i prefer first approach, other 2 approaches improvement.
here example of second approach:
var routevaluesfornextpage = new { page = @viewbag.nextpage, searchterm = request["searchterm"], searchcolumn = request["searchcolumn"], searchorder = request["searchorder"], searchdescending = (request["searchdescending"] ?? "true").tolower().indexof("true") > -1, requeststatus0 = (request["requeststatus0"] ?? "false").tolower().indexof("true") > -1, requeststatus1 = (request["requeststatus1"] ?? "true").tolower().indexof("true") > -1, requeststatus3 = (request["requeststatus3"] ?? "true").tolower().indexof("true") > -1, requeststatus5 = (request["requeststatus5"] ?? "true").tolower().indexof("true") > -1, requeststatus7 = (request["requeststatus7"] ?? "true").tolower().indexof("true") > -1, requeststatus9 = (request["requeststatus9"] ?? "true").tolower().indexof("true") > -1 }; var routevaluesforautorefresh = new { routevaluesfornextpage , lastrefresh = @viewbag.lastrefresh };
here example of third approach:
var page = @viewbag.nextpage; var searchterm = request["searchterm"]; var searchcolumn = request["searchcolumn"]; var searchorder = request["searchorder"]; var searchdescending = (request["searchdescending"] ?? "true").tolower().indexof("true") > -1; var requeststatus0 = (request["requeststatus0"] ?? "false").tolower().indexof("true") > -1; var requeststatus1 = (request["requeststatus1"] ?? "true").tolower().indexof("true") > -1; var requeststatus3 = (request["requeststatus3"] ?? "true").tolower().indexof("true") > -1; var requeststatus5 = (request["requeststatus5"] ?? "true").tolower().indexof("true") > -1; var requeststatus7 = (request["requeststatus7"] ?? "true").tolower().indexof("true") > -1; var requeststatus9 = (request["requeststatus9"] ?? "true").tolower().indexof("true") > -1; var routevaluesforautorefresh = new { page, lastrefresh = @viewbag.lastrefresh, searchterm, searchcolumn, searchorder, searchdescending, requeststatus0, requeststatus1, requeststatus3, requeststatus5, requeststatus7, requeststatus9 }; var routevaluesfornextpage = new { page, searchterm, searchcolumn, searchorder, searchdescending, requeststatus0, requeststatus1, requeststatus3, requeststatus5, requeststatus7, requeststatus9 };
Comments
Post a Comment