c# - LINQ lambda expression throws Sequence contains no elements -
i know error means, not sure how prepare in linq statements.
scannermessages dictionary . values in either numeric serial scan, or string.empty, or (before scans happen) null. have feeling utilize firstordefault(), can't utilize default value of null/string.empty. tried using
.any(x => !string.isnullorempty(x.value)). but returns bool , wrong check. sense need where() tried no avail (i tried changing groupby() clauses). sense close not sure modify expressions.
note: within lock know none of other threads affecting dictionary while querying against (scannermessages locked)
public scantype equalmessages() { seek { lock (lckobj) { if (_scannerspresent) { // check see if message came through caught in buffer. if has equal previous message. if (scannermessages.groupby(x => x.value).where(x => x.count() > 1).first().key == _holdmsg) homecoming scantype.clearbuffer; // if messages null or empty no scan else if (scannermessages.values.all(s => string.isnullorempty(s))) homecoming scantype.noscan; // if there 1 scanner scans must determined either matnum or rescan of lpn else if (_scannercount == 1) homecoming scantype.discernscantype; // if distinct count of non null/empty values less total count of non null/empty values, scan else if (scannermessages.values.distinct().count(v => !string.isnullorempty(v)) < scannermessages.values.count(v => !string.isnullorempty(v))) { // status met if there more 1 of same scan, , hence not reprint. pad values '0's proper matnum format scannermessages = scannermessages.where(x => !string.isnullorempty(x.value)).todictionary(x => x.key, x => x.value.padleft(18, '0')); homecoming scantype.goodscan; } // if non null/empty counts equal one, , message not same previous scan, message not buffer , lpn reprint else if ((scannermessages.values.distinct().count(v => !string.isnullorempty(v)) == 1) && (scannermessages.groupby(x => x.value).where(x => x.count() > 1).first().key != _holdmsg)) homecoming scantype.reprint; else homecoming scantype.noscan; } homecoming scantype.noscan; } } grab (exception ex) { myevents.raiseerrormessagebox(ex.message); throw new exception(ex.message, ex); } } edit: not sure line throwing error- think either first if line or final else if before else homecoming scantype.noscan. statements using first() call. problem gets called every half sec debugging backs up. there 3 threads accessing scannermessages dictionary, , thread calls checking dictionary. not sure how debug when have 4 threads hitting it. thing know is* thread-safe :/ if seek set breakpoint on first line, hits many times while i'm stepping through miss "real" scans.
scannermessages.groupby(x => x.value).where(x => x.count() > 1).first()
so if there no grouping contains more 1 element? think should happen?
you should separate conditions smaller steps instead of trying set in single expression. in case recommend using firstordefault , and replace status :
var value = scannermessages.groupby(x => x.value).where(x => x.count() > 1).firstordefault() if (value != null && value.key == _holdmsg) homecoming scantype.clearbuffer; c# linq lambda
No comments:
Post a Comment