none
checking object count() in lambda expression RRS feed

  • Question

  • Given the following expression is it possible to check the .Count() of v inline prior to using it? In other words how do I confirm that v has at least 1 item without dropping out of the lambda expression?

    response.sa

    .Where(a => a.kv.Where(b => b.k.ToLowerInvariant().Equals(

    "category".ToLowerInvariant()))

    .FirstOrDefault().v[0] == category).ToList();

    Friday, May 14, 2010 6:56 PM

Answers

  • Hi Joey,

    Is it a LINQ to Objects query?   If so, how about such a query?

    ================================================================
    response.sa.Where(a => a.kv.Where(b => b.k.ToLowerInvariant().Equals(
    “category”.ToLowerInvariant())).FirstOrDefault().v.Count() >= 1 &&
    a.kv.Where(b => b.k.ToLowerInvariant().Equals(
    “category”.ToLowerInvariant())).FirstOrDefault().v[0] == category).ToList();
    ================================================================

    Here we first execute the .Count() method to test it whether the count of v is larger than 1 and then access the first element of v.  

     

    Another query would be:

    ================================================================
    response.sa.Where(a => a.kv.Where(b => b.k.ToLowerInvariant().Equals(
    “category”.ToLowerInvariant())).FirstOrDefault().v.Count() >= 1 ?  
    a.kv.Where(b => b.k.ToLowerInvariant().Equals(
    “category”.ToLowerInvariant())).FirstOrDefault().v[0] == category : false).ToList();
    ================================================================

    Here I used the conditional operator ? : to test whether v.Count() is larger than 1.  


    If these methods do not solve the problem, please provide us with more detailed information of your query, I will do my best to help you.

     

    Have a nice weekend!

    Lingzhi Sun
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, May 15, 2010 9:31 AM
    Moderator
  • Wouldn't the Any() work as well.  If true then there is at least one element in the IEnumerable.  If false then is empty.
    Thursday, May 20, 2010 7:10 PM
  • List<Item> results = (from item in items
    		from subItem in items.kv
    		where subItem.ToLowerInvariant().Equals
    			("category".ToLowerInvarient())
    		where subItem.v.Count() > 0
    		where subItem.v.First() == "category"
    		select item).ToList();

    I think this would work.  I didn't know what your objects were called so I just filled in some naming

    You probably can do just where subItem.v.FirstOrDefault() == "category"  since it will return null if count is 0, and null != "category"

    Thursday, May 20, 2010 7:25 PM

All replies

  • Hi Joey,

    Is it a LINQ to Objects query?   If so, how about such a query?

    ================================================================
    response.sa.Where(a => a.kv.Where(b => b.k.ToLowerInvariant().Equals(
    “category”.ToLowerInvariant())).FirstOrDefault().v.Count() >= 1 &&
    a.kv.Where(b => b.k.ToLowerInvariant().Equals(
    “category”.ToLowerInvariant())).FirstOrDefault().v[0] == category).ToList();
    ================================================================

    Here we first execute the .Count() method to test it whether the count of v is larger than 1 and then access the first element of v.  

     

    Another query would be:

    ================================================================
    response.sa.Where(a => a.kv.Where(b => b.k.ToLowerInvariant().Equals(
    “category”.ToLowerInvariant())).FirstOrDefault().v.Count() >= 1 ?  
    a.kv.Where(b => b.k.ToLowerInvariant().Equals(
    “category”.ToLowerInvariant())).FirstOrDefault().v[0] == category : false).ToList();
    ================================================================

    Here I used the conditional operator ? : to test whether v.Count() is larger than 1.  


    If these methods do not solve the problem, please provide us with more detailed information of your query, I will do my best to help you.

     

    Have a nice weekend!

    Lingzhi Sun
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, May 15, 2010 9:31 AM
    Moderator
  • Hi Joey,

     

    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!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, May 20, 2010 9:00 AM
    Moderator
  • Wouldn't the Any() work as well.  If true then there is at least one element in the IEnumerable.  If false then is empty.
    Thursday, May 20, 2010 7:10 PM
  • List<Item> results = (from item in items
    		from subItem in items.kv
    		where subItem.ToLowerInvariant().Equals
    			("category".ToLowerInvarient())
    		where subItem.v.Count() > 0
    		where subItem.v.First() == "category"
    		select item).ToList();

    I think this would work.  I didn't know what your objects were called so I just filled in some naming

    You probably can do just where subItem.v.FirstOrDefault() == "category"  since it will return null if count is 0, and null != "category"

    Thursday, May 20, 2010 7:25 PM
  • Thanks, these all seem to work however I chose LitEnders in the end becuase there is no duplication of statements.

    ResponseSection

    se = (

     

    se = (

    se = (

    from item in response.sa

     

    from subItem in item.kv

     

    where subItem.k.ToLowerInvariant().Equals("category".ToLowerInvariant())

     

    where subItem.v.Count() > 0

     

    where subItem.v.First() == category

     

    select item).FirstOrDefault();

    Tuesday, May 25, 2010 9:47 PM