By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,588 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,588 IT Pros & Developers. It's quick & easy.

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

100+
P: 110
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:
Expand|Select|Wrap|Line Numbers
  1. var entry = (from s in context.tblIPAddresses
  2.                          group s by s.ScanID into g
  3.  
  4.                          select g.OrderByDescending(t=>t.DateTime)).First();
  5.  
but then when I try to call its fields like so:
Expand|Select|Wrap|Line Numbers
  1. lblCurrentIP.Text = entry.IPAddress;
  2.             lblNewDateTime.Text = entry.DateTime.ToString("f");
  3.  
it tells me that entry does not contain a value for ipaddress or for datetime.

Any help with this is appreciated.
Dec 1 '10 #1
Share this Question
Share on Google+
1 Reply


P: 55
Maybe something like:
Expand|Select|Wrap|Line Numbers
  1. var entry = (from e in context.TableName
  2.                 select e).OrderByDescending(e => e.ScanID).FirstOrDefault();
-B
Dec 1 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.