none
Handling a Null IEnumerable from a dynamic entity with a following Where Lamda RRS feed

  • Question

  • Admittedly by the time the below code is executed it is really Linq-to-Object call and not sql, but I am drawing a blank and need the help of the forums. The below code simply weeds out a multitiered data class which is a holder for a list of transactions by applying business rules to the initial lots.

     

    var sells = Lots.Where(lt => {Top Level Condition} )
        .Select( lt => new { lotOfTax  = lt,<br/>
              Transactions = lt.Transactions.Where( tr => tr.IsSellType() ) 
                     .Where( tr => {Condition 2} )
                     .Where( tr => {Condition 3})
                     .Where( tr => {Condition 4})
    
               } )<br/>
    
        .Where( item => item.Transactions != null) /*&& (item.Transactions.FirstOrDefault() != null ))*/
    
         ;
    
    

    Ok what is being returned is a IEnumerable where two new entities are created. One with a valid IEnumerable set of Transactions and one without. The problem is that when it hits the final where and tries to hit the IEnumerable Transactions on the object that contains none, it throws an error "Object reference not set to an instance of an object.".

    I can't think of any logic to weed out the dynamic entity which has no sub data Transactions. Thoughts?


    William Wegerson (www.OmegaCoder.Com)
    • Edited by OmegaManModerator Wednesday, October 20, 2010 6:09 PM Removed html tags from bad formmatting from Forums code paster
    Wednesday, October 20, 2010 12:51 PM
    Moderator

Answers

  • var sells = (from lot in Lots
        where {Top Level Condition}
        let transactions = 
           (from transaction in lot.Transactions
               where { Condition 2 }
               where { Condition 3 }
               where { Condition 4 }
               select transaction)
        where transaction.Count() > 0
        select new {
           LotOfTax = lot,
           Transactions = transactions
           });
    

    I am better at linq statement syntax, than method syntax so I switched it.  I would try the above sample.  Of course I wrote it off the top of my head.  I am not sure that you can use the keyword let with a IEnumerable, but if not you can just replace both "transactions" with that sub query.

     

    Wednesday, October 20, 2010 6:21 PM

All replies

  • Just Do ...

    Lots.Where(lt => {Top Level Condition } && lt.Transactions != null)

    Or

    Lots.Where(lt => {Top Level Condition }).Where(lt => lt.Transactions != null)

    Wednesday, October 20, 2010 4:09 PM
  • Hi Lit...the code can't discrimitate at the very first where. Each lot has transaction yes, but those transactions have to be first weeded out by the where conditions 2/3 and 4. The resulting entity may or may not contain transactions due to the weeding out of the *lower level* of the transactions. Thx though...
    William Wegerson (www.OmegaCoder.Com)
    Wednesday, October 20, 2010 6:11 PM
    Moderator
  • var sells = (from lot in Lots
        where {Top Level Condition}
        let transactions = 
           (from transaction in lot.Transactions
               where { Condition 2 }
               where { Condition 3 }
               where { Condition 4 }
               select transaction)
        where transaction.Count() > 0
        select new {
           LotOfTax = lot,
           Transactions = transactions
           });
    

    I am better at linq statement syntax, than method syntax so I switched it.  I would try the above sample.  Of course I wrote it off the top of my head.  I am not sure that you can use the keyword let with a IEnumerable, but if not you can just replace both "transactions" with that sub query.

     

    Wednesday, October 20, 2010 6:21 PM
  • Thanks Lit I would never have thought to add a where to a let statement and learned something new. But it did the trick. Thanks!

     

     


    William Wegerson (www.OmegaCoder.Com)
    Wednesday, October 20, 2010 10:16 PM
    Moderator
  • I moved the project to .Net 4 and was able to step into in from the debugger in VS2010. The real problem is that the test code relies on Mock Objects which I had setup previous section but didn't consider for the Condition tests above.

    So the real object not found was not on the line I thought but on a different line altogether....your code suggestion reversed how the object was traversed masking the problem.

    Thanks for your help it allowed me to view the issue with new eyes and to that I am grateful.


    William Wegerson (www.OmegaCoder.Com)
    Thursday, October 21, 2010 12:52 AM
    Moderator
  • I am glad that I was able to give such insight to a MVP, Moderator.
    Thursday, October 21, 2010 1:35 PM
  • I am glad that I was able to give such insight to a MVP, Moderator.

    I am modest in that I am always learning verses giving the image of knowing everything. :-) The actual problem was an error thrown in other code and that is why I was stumped and posted....but if I hadn't posted I wouldn't have seen your post and I would not have learned. To me that is the crux...I am better off than hiding behind some image; to that end as mentioned thanks.

    William Wegerson (www.OmegaCoder.Com)
    Wednesday, October 27, 2010 2:25 PM
    Moderator