c# - Linq query does not work when compiled -


i have more 100 compiled query 1 causes problem. exact compiled query:

public static func<databasedatacontext, int, string, int, byte, namecommentpageresult>            getnamecomments = compiledquery.compile((databasedatacontext db, int nameid, string userid, int start, byte count)         => new namecommentpageresult         {             count = db.namecomments.count(q => q.nameid == nameid && q.verifiedby != "-1"),             name = db.names.first(n => n.id == nameid).name1,             comments = db.namecomments.where(c => c.nameid == nameid && c.verifiedby != "-1").select(c => new namecommentresult             {                 datetime = c.datetime,                 id = c.id,                 nameid = c.nameid,                 userid = c.userid,                 uservoted = db.namecommentvotes.any(v => v.userid == userid && v.commentid == c.id),                 userdisplayname = db.aspnetuserclaims.any(cl => cl.claimtype == "displayname" && cl.claimvalue != "" && cl.user_id == c.userid) ? db.aspnetuserclaims.where(cl => cl.claimtype == "displayname" && cl.claimvalue != "" && cl.user_id == c.userid).first().claimvalue : db.aspnetusers.where(u => u.id == c.userid).first().username,                 userphoto = db.aspnetuserclaims.where(cl => cl.user_id == c.userid && cl.claimtype == "image").singleordefault().claimvalue,             }).orderbydescending(o => o.datetime).skip(start - 1).take(count).toarray()         }); 

i'm getting error:

value cannot null. parameter name: value

description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code. exception details: system.argumentnullexception: value cannot null. parameter name: value

but when run same code without compiling it, works fine, problem?

this works fine:

int start = 1; byte count = 10; int nameid = 100;      using (databasedatacontext db = new databasedatacontext())                 {                     //result = _names.comments.getnamecomments(db, id, user == null ? "" : user.id, 1, (byte)10);                     result = new namecommentpageresult                        {                            count = db.namecomments.count(q => q.nameid == id && q.verifiedby != "-1"),                            name = db.names.first(n => n.id == id).name1,                            comments = db.namecomments.where(c => c.nameid == id && c.verifiedby != "-1").select(c => new namecommentresult                            {                                datetime = c.datetime,                                id = c.id,                                nameid = c.nameid,                                userid = c.userid,                                uservoted = db.namecommentvotes.any(v => v.userid == user.id && v.commentid == c.id),                                userdisplayname = db.aspnetuserclaims.any(cl => cl.claimtype == "displayname" && cl.claimvalue != "" && cl.user_id == c.userid) ? db.aspnetuserclaims.where(cl => cl.claimtype == "displayname" && cl.claimvalue != "" && cl.user_id == c.userid).first().claimvalue : db.aspnetusers.where(u => u.id == c.userid).first().username,                                userphoto = db.aspnetuserclaims.where(cl => cl.user_id == c.userid && cl.claimtype == "image").singleordefault().claimvalue,                            }).orderbydescending(o => o.datetime).skip(start - 1).take(count).toarray()                        };                 } 

and stack trace:

[argumentnullexception: value cannot null. parameter name: value] system.data.linq.sqlclient.sqljoin..ctor(sqljointype type, sqlsource left, sqlsource right, sqlexpression cond, expression sourceexpression) +1222094 system.data.linq.sqlclient.visitor.visitmultiset(sqlsubselect sms) +324 system.data.linq.sqlclient.sqlvisitor.visitsubselect(sqlsubselect ss) +91 system.data.linq.sqlclient.sqlvisitor.visit(sqlnode node) +1014 system.data.linq.sqlclient.sqlvisitor.visitexpression(sqlexpression exp) +15 system.data.linq.sqlclient.sqlvisitor.visitnew(sqlnew sox) +186 system.data.linq.sqlclient.sqlvisitor.visit(sqlnode node) +1205 system.data.linq.sqlclient.sqlvisitor.visitexpression(sqlexpression exp) +15 system.data.linq.sqlclient.visitor.visitselect(sqlselect select) +128 system.data.linq.sqlclient.sqlvisitor.visit(sqlnode node) +1110 system.data.linq.sqlclient.sqlprovider.buildquery(resultshape resultshape, type resulttype, sqlnode node, readonlycollection`1 parentparameters, sqlnodeannotations annotations) +828 system.data.linq.sqlclient.sqlprovider.buildquery(expression query, sqlnodeannotations annotations) +279 system.data.linq.sqlclient.sqlprovider.system.data.linq.provider.iprovider.compile(expression query) +104 system.data.linq.compiledquery.executequery(datacontext context, object[] args) +203 system.data.linq.compiledquery.invoke(targ0 arg0, targ1 arg1, targ2 arg2, targ3 arg3, targ4 arg4) +223 comments.page_load(object sender, eventargs e) in g:\dropbox\projects\namebabies-new\comments.aspx.cs:21 system.web.ui.control.loadrecursive() +71 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +3178

update:

this funny code solves error not right thing do, can suggest do instead?

public static func<databasedatacontext, int, string, int, byte, namecommentpageresult>                getnamecomments = compiledquery.compile((databasedatacontext db, int nameid, string userid, int start, byte count)           db.names.select(s  => new namecommentpageresult             {                 count = db.namecomments.count(q => q.nameid == nameid && q.verifiedby != "-1"),                 name = db.names.first(n => n.id == nameid).name1,                 comments = db.namecomments.where(c => c.nameid == nameid && c.verifiedby != "-1").select(c => new namecommentresult                 {                     datetime = c.datetime,                     id = c.id,                     nameid = c.nameid,                     userid = c.userid,                     uservoted = db.namecommentvotes.any(v => v.userid == userid && v.commentid == c.id),                     userdisplayname = db.aspnetuserclaims.any(cl => cl.claimtype == "displayname" && cl.claimvalue != "" && cl.user_id == c.userid) ? db.aspnetuserclaims.where(cl => cl.claimtype == "displayname" && cl.claimvalue != "" && cl.user_id == c.userid).first().claimvalue : db.aspnetusers.where(u => u.id == c.userid).first().username,                     userphoto = db.aspnetuserclaims.where(cl => cl.user_id == c.userid && cl.claimtype == "image").singleordefault().claimvalue,                 }).orderbydescending(o => o.datetime).skip(start - 1).take(count).toarray()             }).first()); 

i edited => new namecommentpageresult db.names.select(s => new namecommentpageresult , getting first result. please note db.names irrelevant query , can use db.anytablename instead

this l2s bug clear fact internal l2s code crashing in uncontrolled way.

that said querying sub-collection (here: comments) either slow when works (select n+1) or not supported. probably, it's not supported in compiled queries. compiled queries not support constructs.

probably, should not query sub-collection anyway because of performance concerns. rewrite code not being done.

since maybe learning .net right now: linq sql obsolete , aspx pages considered obsolete (or technology of last resort) many.


Comments