c# - Operator '!=' cannot be applied to operands of type -
so have controller implementing search file in database. user can optionally filter search 2 fields, organization , category, or can leave dropdown menus "all".
i have 3 tables work with:
(1)
orgs --------------------- orgid | orgname (2)
cats --------------------- orgid | orgname (3)
files ------------------------------------------ fileid | orgid | catid | filename and file name they're searching for. narrow down results, i'm working mirror of files database , deleting rows it:
[httppost] public actionresult assetsearch (string selectedorgname, string selectedcatname, string searchval) { portaldata pd = new portaldata(); if (selectedorgname != "all") { var selectedorgid = pd.orgs.firstordefault(o => o.orgname == selectedorgname); // orgid corresponding selected organization if (selectedorgid == null) return content("couldn't find row organization '" + selectedorgname + "' in database."); // return error message if no such oranization name exists (from thisfile in pd.orgs thisfile.orgid != selectedorgid select thisfile).delete(); // delete rows don't have orgid corresponding selected organization } // if search filtered specific organization if (selectedcatname != "all") { var selectedcatid = pd.cats.firstordefault(c => c.catname == selectedcatname); // catid corresponding selected category if (selectedcatid == null) return content("couldn't find row category '" + selectedcatname + "' in database."); // return error message if no such organization name exists (from thisfile in pd.orgs thisfile != selectedcatid select thisfile).delete(); } // if search filtered specfic category my first question has error mentioned in title. error pointing
thisfile.orgid != selectedorgid and
thisfile.orgid != selectedcatid i'm not sure why doesn't expressions, considering orgids in sql database ints , should value returned pd.orgs.firstordefault(o => o.orgname == selectedorgname).
my next question how can make messy code above more compact, efficient, readable , reliable.
(i'm n00b c#/linq/etc., go easy on me!)
var selectedorgid = pd.orgs.firstordefault(o => o.orgname == selectedorgname); will of entity type "organisation", not it's id, same
var selectedcatid = pd.cats.firstordefault(c => c.catname == selectedcatname); so need select id this:
var selectedorgid = pd.orgs.firstordefault(o => o.orgname == selectedorgname).orgid; i'd suggest check null there, result of query
pd.orgs.firstordefault(o => o.orgname == selectedorgname) is not null
Comments
Post a Comment