where not found.

Apr 25, 2011 at 1:56 PM

Hi, 

what happen where can't found any ltems ?, 

 

'taskhost.exe' (Managed): Loaded 'System.SR.dll'
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll

this is what i got, when where can't found any item. what method should i use to prevent this ?

thanks

Coordinator
Apr 25, 2011 at 2:21 PM

Need more details. Not sure what you're asking.

Apr 25, 2011 at 2:58 PM

sorry, this is the scenario. 

i'm looking for category named "tiger", using where syntax. and where cannot find category named tiger. and the application will go crash with error code as above.

my query is,

  catname = "tiger"

 categories = SterlingService.Current.Database.Query<Category, int>()
                    .Where(delegate(TableKey<Category, int> key) { return key.LazyValue.Value.categoryName == catname; })
                    .First<TableKey<Category, int>>()
                    .LazyValue.Value;

 

and there are no error, if i have category named "tiger".

I hope you got what i mean. thanks

Coordinator
Apr 25, 2011 at 3:00 PM

You are demanding that the query return a value, so it will crash if there is not one. If you want to allow for not having a value, you'll need FirstOrDefault, not First.

Apr 25, 2011 at 3:13 PM
Edited Apr 25, 2011 at 4:06 PM

hahaha, sorry jeremy. 

i just need to google "firstordefault" first, and learn from that.