none
problem with where != null RRS feed

  • Question

  • I'm actually having a couple of problems with this query:

    this.comboBoxLatchKey.ItemsSource = from cmd in RmcAct.CommandList
                      where cmd.Key != null && cmd.Key.Length > 0
                      select cmd.Key;
    
    

    First, the query is not returning cmd.Key values. It appears to be returning complete objects from the static CommandList. And secondly, it retruns every oject in the list, regardless of whether or not cmd.Key is null.

    Why is this not working?

    Thanks,

     


    -- Bill McCormick ACE-CO
    Friday, December 17, 2010 5:26 PM

Answers

  • Something else is amiss...step through your Linq in the debugger for this works fine:

    class TestCL { public string Key { get; set; } public string Other { get; set; } }
    
    
    List<TestCL> theList = new List<TestCL>() { new TestCL() { Key="Main", Other="Jabberwocky" },
                          new TestCL() { Other="NoKey" },
                          new TestCL() { Key="OmegaMan", Other="Omega" } };
    
    var result = theList.Where( it => string.IsNullOrEmpty( it.Key ) == false)
              .Select( it => it.Key);
    
    foreach(var item in result)
      Console.WriteLine(item);
    /* output:
    Main
    OmegaMan
    */ 
    

    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by liurong luo Monday, December 27, 2010 4:16 AM
    Friday, December 17, 2010 8:05 PM
    Moderator

All replies

  • Something else is amiss...step through your Linq in the debugger for this works fine:

    class TestCL { public string Key { get; set; } public string Other { get; set; } }
    
    
    List<TestCL> theList = new List<TestCL>() { new TestCL() { Key="Main", Other="Jabberwocky" },
                          new TestCL() { Other="NoKey" },
                          new TestCL() { Key="OmegaMan", Other="Omega" } };
    
    var result = theList.Where( it => string.IsNullOrEmpty( it.Key ) == false)
              .Select( it => it.Key);
    
    foreach(var item in result)
      Console.WriteLine(item);
    /* output:
    Main
    OmegaMan
    */ 
    

    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by liurong luo Monday, December 27, 2010 4:16 AM
    Friday, December 17, 2010 8:05 PM
    Moderator
  • I made the following change:

    this.comboBoxLatchKey.ItemsSource = from cmd in RmcAct.CommandList
             where cmd.Key != null && cmd.Key.Length > 0
             orderby cmd.Key
             select cmd.Key;
    

    This gives me what I want, but the other Linq should work as well, I would think.

    Using string.IsNullOrEmpty had no effect. See what happens if you set Key=null for your "NoKey" record. I think that would more closely match my scenario.

    Thanks,

     


    -- Bill McCormick ACE-CO
    Friday, December 17, 2010 9:25 PM
  • I took your suggestion and set it to Null...no change in behavoir. Due to lazy loading is commandlist being changed by the time its ultimately used? Maybe saying CommandList.ToList().Where is what needs to be checked...HTH
    William Wegerson (www.OmegaCoder.Com)
    Friday, December 17, 2010 11:36 PM
    Moderator
  • Hello Bill,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day!


    Roahn Luo [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 20, 2010 2:55 AM
  • Thanks for the follow up.

    I think that commandlist is being changed by the time its ultimately used.

    Adding the orderby solved the problem.

     

    Thanks,

     


    -- Bill McCormick ACE-CO
    Tuesday, January 4, 2011 7:45 PM