As to the why, it's because input = db.table.FirstOrDefault()
is a statement and doesn't actually return anything. It assigns something to the input
variable. As it doesn't return anything (i.e. it's a statement), you cannot compare it to something else.
The if
expects an expression (something that returns something). Here's more on the difference between statements and expressions.
This is different from C where (if I'm not mistaken) everything that has a value of 0 if false
and everything else is true
.
As to the closest you can get with C#, I believe you can do something like:
MyClass input = null;
if ((input = db.table.FirstOrDefault()) != null)
{
// use input here
}
But then you might as well do:
MyClass input = db.table.FirstOrDefault();
if (input != null)
{
// use input here
}