How do I select the last entry in my database in LINQ to SQL?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuzz13
    New Member
    • Jun 2010
    • 110

    #1

    How do I select the last entry in my database in LINQ to SQL?

    I have 2 fields that will be inserted into my database ip address and datetime. Each time they are entered they are assigned a ScanID primary key so the value should increment with each entry. How do I select the last entry, the one with the highest ScanID or the one with the most current datetime?

    I thought it would look like this:
    Code:
    var entry = (from s in context.tblIPAddresses
                             group s by s.ScanID into g
                             
                             select g.OrderByDescending(t=>t.DateTime)).First();
    but then when I try to call its fields like so:
    Code:
    lblCurrentIP.Text = entry.IPAddress;
                lblNewDateTime.Text = entry.DateTime.ToString("f");
    it tells me that entry does not contain a value for ipaddress or for datetime.

    Any help with this is appreciated.
  • Bryan Cheung
    New Member
    • Nov 2010
    • 55

    #2
    Maybe something like:
    Code:
    var entry = (from e in context.TableName
    				select e).OrderByDescending(e => e.ScanID).FirstOrDefault();
    -B

    Comment

    Working...