c# - Sequence contains no elements -
can explain me why following linq query throws invalidoperationexception?
(don't list has no elements,the value i'm looking exists in collection)
class program { static int lastnumber; static void main() { int loopcount = 100; int minvalue = 1; int maxvalue = 10000; var numbers = enumerable.range(minvalue, maxvalue).tolist();//or toarray(); random random = new random(); (int = 0; < loopcount; i++) { //.first() throws exception obvious value exists in list int x = numbers.where(v => v == newmethod(minvalue, maxvalue, random)).first(); } console.writeline("finished"); console.readline(); } private static int newmethod(int minvalue, int maxvalue, random random) { var a1 = random.next(minvalue + 1, maxvalue - 1); lastnumber = a1; return a1; } }
the problem appears when call newmethod inside lambda expession.
if works
int temp=newmethod(minvalue, maxvalue, random); int x = numbers.where(v => v == temp).first();
i added lastnumber field debug code,you can see value exists in collection when crashes
ps
problem not random variable,i removed parameter , create new local random inside method problem still exists
update
it turns out don't need loop make crash. if run program many times error again
using system; using system.collections.generic; using system.diagnostics; using system.linq; class program { static int lastnumber; static void main() { int minvalue = 1, maxvalue = 100000; var numbers = enumerable.range(minvalue, maxvalue).toarray(); //crashes int x = numbers.where(v => v == newmethod(minvalue, maxvalue)).first(); console.writeline("finished"); console.readline(); } private static int newmethod(int minvalue, int maxvalue) { random random = new random(); var a1 = random.next(minvalue + 1, maxvalue - 1); lastnumber = a1; return a1; } }
@oleg right, here's why it's problem.
where
scans through list looking elements match given criteria. in case the criteria changes each element. if made problem smaller, array of 5 elements:
list<int32> (5 items) 1 2 3 4 5
and looped through looking value matches random number (x item[i]
, r
random number):
item 1: x = 1, r = 2 // fail item 2: x = 2, r = 3 // fail item 3: x = 3, r = 2 // fail item 4: x = 4, r = 3 // fail item 5: x = 5, r = 2 // fail
note no item match particular random number, no item matched criteria , first
throws exception!
the fix, have discovered, generate random number before enumeration:
int temp=newmethod(minvalue, maxvalue, random); // 2 item 1: x = 1, temp = 2 // fail item 2: x = 2, temp = 2 // success!
side note:
it's bit misleading use maxvalue
here:
enumerable.range(minvalue, maxvalue)
since second parameter enumerable.range
length of resulting collection, not max value. in case works because you're starting @ 1 results unexpected if used, 99 , 100 - you'd range 99 198.
Comments
Post a Comment