From: shapper on
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
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
}