Wednesday, 15 January 2014

c# - Operator '&&' cannot be applied to operands of type bool 'System.Linq' and search, sort and paging -



c# - Operator '&&' cannot be applied to operands of type bool 'System.Linq' and search, sort and paging -

i know question has been asked before think case little more unique, why i'm here asking help.

so have table, serverdb_ contains values serverid (pk, fk), databaseid (pk, fk) instancename , verified. readability sake user, wanted show servername , databasename server_ table, , database_ table, , allow user sort , search these parameters well.

this tutorial has been godsend, i'm finding it's limited.

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

this code, not right , gets compiler errors. i've messed around , played lot, i'm kinda stuck. i'm not sure how can still sort, search , lastly paging alternative work, different table values. paging gives me exception because of other var values added.

public actionresult index(string sortorder, string searchstring, int? page) { viewbag.currentsort = sortorder; viewbag.idsortparm = string.isnullorempty(sortorder) ? "serverid_desc" : ""; viewbag.id2sortparm = sortorder == "id2" ? "databaseid_desc" : "id"; viewbag.namesortparm = sortorder == "name" ? "servername_desc" : "name"; viewbag.name2sortparm = sortorder == "name2" ? "databasename_desc" : "name2"; if (request.httpmethod != "get") { page = 1; } viewbag.currentfilter = searchstring; var serverdb= s in db.serverdb_ //original var serverdb select s; var server = x in db.server_ //created server_ values select x; var database = y in db.database_ //created database_ values select y; if (!string.isnullorempty(searchstring)) //newserverdb made hold values different tables { var newserverdb = (serverdb.where(s => s.serverid.tostring().contains(searchstring.toupper()) &&(server.where(x => x.servername.toupper().contains(searchstring.toupper()))); } switch (sortorder) { case "serverid_desc": serverdb = serverdb.orderbydescending(s => s.serverid); break; case "id2": serverdb = serverdb.orderby(s=> s.databaseid); break; case "databaseid_desc": serverdb = serverdb.orderbydescending(s => s.databaseid); break; case "name": server = server.orderby(x => x.servername); break; case "servername_desc": server = server.orderbydescending(x => x.servername); break; case "name2": database = database.orderby(y => y.databasename); break; case "databasename_desc": database = database.orderbydescending(y => y.databasename); break; default: serverdb = serverdb.orderby(s =>s.serverid); break; } int pagesize = 20; int pagenumber = (page ?? 1); homecoming view(newserverdb.topagedlist(pagenumber, pagesize)); }

i'm sorry if question kind of hard understand. tried set eloquently possible, stuff hard understand.

thanks guys!!

your sec part of status homecoming ienumerable<t>

server.where(x => x.servername.toupper().contains(searchstring.toupper()))

whereas operator && requires boolean value on right hand side.

use enumerable.any like:

var newserverdb = (serverdb.where(s => s.serverid.tostring().contains(searchstring.toupper()) &&(server.any(x => x.servername.toupper().contains(searchstring.toupper()))); //^^^^^

i not sure how utilize query later in code, since have defined within if block , visible there. later when do:

return view(newserverdb.topagedlist(pagenumber, pagesize));

you error since newserverdb not visible here.

you can declare iqueryable<serverdb_> newserverdb outside of if block , later utilize that, or can move whole block @ end of method, , homecoming within of if block.

c# asp.net asp.net-mvc linq

No comments:

Post a Comment