none
Get entity with earliest date RRS feed

  • Question

  • I'm not very experienced at LINQ, but I think this is probably a simple question.

    I have an EntityCollection and I need to get the entity from the collection that has the earliest date.

    Dim q = From oBal In Me.Balances Where oBal.dtBalance = Me.Balances.Min(Function(z) z.dtBalance)
    

    Is this correct, and/or the best way to do this?

    Thanks,
    Josh

     

    Friday, May 27, 2011 9:11 PM

Answers

  • Hi Josh;

    This is how I would do it, I order the collection by the date, ascending order, and then return the first in the list.

    Dim qResult = (From b In Me.Balances
            Order By b.dtBalance Ascending
            Select b).FirstOrDefault()
    
    

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, May 28, 2011 2:48 AM

  • I think joshSommers is right.
    and the default value for order by is asc
    so I think it is no need to wirte "ascending"

    Dim qResult = (From b In Me.Balances
        Order By b.dtBalance 
        Select b).FirstOrDefault()
    

    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, May 28, 2011 7:46 AM

All replies

  • Hi Josh;

    This is how I would do it, I order the collection by the date, ascending order, and then return the first in the list.

    Dim qResult = (From b In Me.Balances
            Order By b.dtBalance Ascending
            Select b).FirstOrDefault()
    
    

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, May 28, 2011 2:48 AM

  • I think joshSommers is right.
    and the default value for order by is asc
    so I think it is no need to wirte "ascending"

    Dim qResult = (From b In Me.Balances
        Order By b.dtBalance 
        Select b).FirstOrDefault()
    

    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, May 28, 2011 7:46 AM
  • @ Tim;

    I think joshSommers is right. and the default value for order by is asc so I think it is no need to wirte "ascending"

     I think you may have gotten the names mixed up in your post because all you did was to remove the keyword Ascending, which is not needed but is great for documentation, from my post. It also helps those who are not up on Linq that you can state Ascending and if you can state that then maybe you can state Descending.

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, May 28, 2011 6:06 PM