Wednesday, January 20, 2010

Error : Sequence contains no elements when you execute query that return null

In this statement if Trans.param1== param1Value returns nothing, you will hit the error above.

Class1 objClass1 = _db.Class1.First(Trans => Trans.param1== param1Value);

To prevent this, you need to use 'FirstOrDefault' instead

Class1 objClass1 = _db.Class1.FirstOrDefault(Trans => Trans.param1== param1Value);

No comments:

Post a Comment