none
Nullable<short> equal 0 Problem RRS feed

  • Question

  • Hi friends;

    My entity variable is as follows

    public Nullable<short> x { get; set; }

    When I used this variable in c# like;

      from.... where.....
        (a.x== 0 ? true : false)

    select..........

    although   a.x is  not "0" linq query return true,

    I cant understand why ?


    • Edited by fibonecci1 Tuesday, April 28, 2015 12:55 PM
    Tuesday, April 28, 2015 12:44 PM

All replies

  • Hello fibonecci1,

    It is confused I cannot reproduce the scenario you mentioned, I made tests by using LINQ to Objects as

    List<SC20150429> scs = new List<SC20150429>() { new SC20150429() { ID = 1, X = 1 }, new SC20150429() { ID = 2, X = null }, new SC20150429() { ID = 3 } };
    
    
                    foreach (var s in scs)
    
                    {
    
                        Console.WriteLine("If X is 0:{0}", s.X == 0 ? true : false);
    
                    }

    And using LINQ to SQL:

    using (DataClasses1DataContext db = new DataClasses1DataContext())
    
                    {
    
                        var result = (from a in db.T20150429s
    
                                      where (a.Date == 0 ? true : false) == true
    
                                      select a).ToList();
    
                    }

    Both of these tests give excepted results, if the X/Data is not 0, it return false.

    >> although   a.x is  not "0" linq query return true,

    Do you mean that whatever the a.x is, it returns true always? I suggest you could provide your detail entity class and the complete query so that we can help you further.

    Or have a try with my code with the environment:.NET 4.5, windows 8.1 and VS2013.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, April 29, 2015 3:24 AM
    Moderator
  • Hello;

    Hello; Thank you for reply ..But still I cant solve this problem

    My table column like that;

    column = smallint, null

    My entity column like that;

    public Nullable<short> entitycolumn { get; set; }

    I used columns in my code like that;

     where (a.entitycolumn != 0 ? true : false)

    a.entitycolumn is 0 but not returned false always return true, 

    Wednesday, April 29, 2015 7:11 AM
  • If possible, please share your table definition and your whole code or you could upload your project to onedrive so people on this forum could download it and test it to see if they could help on this issue.
    Wednesday, May 6, 2015 10:01 AM
  • Hi,

    i'm tried your scenario with the below example. But, it's return proper result only. Please share your code with data, so that i could able to resolve

    public class Test
        {
            public Nullable<short> x { get; set; }
        } 

     List<Test> collection = new List<Test>();
                    collection.Add(new Test() { x = 0});
                    collection.Add(new Test());
                    collection.Add(new Test() { x = 0 });
                    collection.Add(new Test() { x = 4 });
                   var col =  collection.Where(x => x.x != 0 ? true : false);

    Friday, May 22, 2015 9:31 AM