c# - Filter Linq with Guid Stored in Session -
i using code filter linq data , keeps crashing.
public actionresult index() { var model = db.tickets .include(t => t.ticketnotes) .where(t => t.openuserid == guid.parse(session["logeduserid"] string)) .orderby(t => t.opendate) .tolist(); return view(model); }
the error is:
an exception of type 'system.notsupportedexception' occurred in entityframework.sqlserver.dll not handled in user code
the error know on line:
.where(t => t.openuserid == guid.parse(session["logeduserid"] string))
because working when guid hardcoded in case:
public actionresult index() { var model = db.tickets .include(t=>t.ticketnotes) .where(t.openuserid == new guid("00000000-0000-0000-0000-000000000000")) .orderby(t=>t.opendate); return view(model); }
you error because linq code transformed in sql. in sql method guid.parse
don't exists.
in example where(t.openuserid == new guid("00000000-0000-0000-0000-000000000000"))
you not parsing anything (calling method), create new guid
supported.
a way parse
guid
outside where
.
var userid = guid.parse(session["logeduserid"] string); // conversion done outside var model = db.tickets .include(t => t.ticketnotes) .where(t => t.openuserid == userid) .orderby(t => t.opendate) .tolist();
another easy way fix adding tolist()
. @entropic mentioned enumerate through entire table , not recommended large tables.
var model = db.tickets .include(t => t.ticketnotes) .tolist() // < added here .where(t => t.openuserid == guid.parse(session["logeduserid"] string)) .orderby(t => t.opendate) .tolist();
Comments
Post a Comment