Prev: Are all types derived from system.object referenced types
Next: A Complete multithreaded console program that is using severalapp domains
From: shapper on 18 May 2010 14:55 Hello, I am getting a record as follows: User user = _context.Users.FirstOrDefault(u => u.Id == task.User.Id); How can I make user to be null if no user is found with such an id. At the moment I get an exception ... If I use only First I also get an exception. Thanks, Miguel
From: Mr. Arnold on 18 May 2010 15:14
shapper wrote: > Hello, > > I am getting a record as follows: > User user = _context.Users.FirstOrDefault(u => u.Id == > task.User.Id); > > How can I make user to be null if no user is found with such an id. > At the moment I get an exception ... If I use only First I also get an > exception. > Doesn't user come back null on the 'Default'? That's what the FirstorDefault does is set the object to null if criteria not found. if (user !null) { do something with object } else { do something if object is null } |