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

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

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
  4.                          select g.OrderByDescending(t=>t.DateTime)).First();
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");
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();
Dec 1 '10 #2

Post your reply

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