locked
How can query.ToList() always fetch a type other than null... warning CS0472: The result of the expression is always 'true' s RRS feed

  • Question

  • Hello,

    Want to know more about the compile time warning

     warning CS0472: The result of the expression is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?'

    It is originating from these statements:

                        if (_result != null)
                            _entityID = _result;

    Obviously the query results may or may not have values based on the parameter value, so how does VS assume it is always true?

               Int32 _entityID = -1;

                    using (var ctx = new MyEntities())
                    {
                        var query = from em in ctx.EntityMasters
                                    join en in ctx.EntityNames
                                    on em.EntityID equals en.EntityID
                                    where en.EName == _entityName
                                    select em.EntityID;
                        var _result = query.ToList().FirstOrDefault();
                        if (_result != null)
                            _entityID = _result;

                        return _entityID;
                 }

    Monday, January 23, 2012 7:46 PM

Answers

  • Hi Sureshg;

    I suspect that the column EntityID in the database is of type int. When this line of code is evaluated :

    var _result = query.ToList().FirstOrDefault();

    _result is typed as int because that is what is being returned from the query->List(). When you test _result with this statement :

     if (_result != null)

    _result can never be null because the basic data type such as int can never have a null value assign to it. Only objects data types can be null such as data types created from a class. So because it can never be null the statement is always true.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Sureshg Monday, January 23, 2012 8:46 PM
    Monday, January 23, 2012 8:14 PM

All replies

  • Hi Sureshg;

    I suspect that the column EntityID in the database is of type int. When this line of code is evaluated :

    var _result = query.ToList().FirstOrDefault();

    _result is typed as int because that is what is being returned from the query->List(). When you test _result with this statement :

     if (_result != null)

    _result can never be null because the basic data type such as int can never have a null value assign to it. Only objects data types can be null such as data types created from a class. So because it can never be null the statement is always true.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Sureshg Monday, January 23, 2012 8:46 PM
    Monday, January 23, 2012 8:14 PM
  • Hi Sureshg;

    Also this statement :

    var _result = query.ToList().FirstOrDefault();

    is not very efficient because it returns a collection and then returns the first in the list. Where this statement:

    var _result = query.FirstOrDefault();

    just returns a single value from the database.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, January 23, 2012 8:19 PM
  • Thank You for the reply. Yes EntityID is an integer in the database, but what does the query return when it has no matching records?

    Monday, January 23, 2012 8:24 PM
  • Thank You for  the suggestion, I'll have my code fixed.
    Monday, January 23, 2012 8:25 PM
  • I got it, FirstOrDefault() returns the default value of the object when no records are returned from the database. So my checking for integer values should have been

    if (_result != 0)
    _entityID = _result;

    Monday, January 23, 2012 8:54 PM
  •  

    Yes that is correct. The query will return an integer but when no records are returned it will return a default value and the default value for an integer is zero.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, January 23, 2012 9:21 PM