473,803 Members | 3,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

110 New Member
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
1 6794
Bryan Cheung
55 New Member
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

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

Similar topics

3
1461
by: eric404 | last post by:
(I don't post here often, so in case I'm violating long-standing taboos of this newsgroup, I apologize in advance for calling a relation a table, using nulls, and other ignorant, destructive, and comtemptible terminology.) I have a table that's keeping a sort of running log of different types of changes to pieces of data. The table has a foreign key of the data being changed, the foreign key for the type of change occuring, some...
2
8830
by: PC | last post by:
Hi, I have used the DLast("!","")function to find and display the last entry in a table. How would I go about displaying the last record in a table that is not Null? Thanks in advance ...pc
11
12485
by: David Messner | last post by:
Ok I know this is simple but the statement eludes me... I have a date field where I want the default value on the data entry form's date field to be the last date entered. I figure I can do this with a query but don't know what the criteria needs to be. Any help would be appreciated, Thanks
2
1421
by: Stefan Richter | last post by:
How can I dynamically select a ComboBox Entry? Could you please provide some sample code? Thanks in advance!!! Stefan
5
2436
by: John Veldthuis | last post by:
My code works perfectly 100% when adding items to my ArrayList and updating the listbox. Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is the last entry and you select another index it goes boom!!!. Funny thing is the SelectedIndex Changed routine fires off perfectly and then crash after the return from this code. Now looking around the news groups it appears to be a bug in the .net...
1
2009
by: Sakakini | last post by:
How can I append last entry from one table to another table???
5
12846
by: Wernerh | last post by:
Hi, I have created a log in a listbox to show users everytime someone has logged on. Is there a form load function that will scroll down the listbox to the last entry each time the form is loaded?? Thanks
16
4320
by: vjayis | last post by:
hi i have a xml file which has transaction details of the users. I m inserting data using appendChild() from php. While displaying i need to display the last 5 entry or single entry or the whole entries descending using php. Is there anyway to read the xml file from the lastentry using php...
1
3066
by: karuppiah | last post by:
Hi All, i need query,how to select last 3 month ,last 3wek,last 3 days , this last 3 month and 3 week should be friday, using sysdate from dual table, there is no exsting table, pls any one help me. Regards, karuppiah.
2
2433
by: minijus | last post by:
Hello, I have a query like this: SELECT jos_vm_orders.user_id, jos_vm_orders.cdate, sum( order_total ) AS total, count( jos_vm_orders.order_id ) AS ordercount, jos_vm_user_info.first_name, jos_vm_user_info.last_name, jos_vm_user_info.phone_1, jos_vm_user_info.user_email, jos_vm_user_info.city FROM jos_vm_orders LEFT JOIN jos_vm_user_info ON jos_vm_orders.user_id = jos_vm_user_info.user_id WHERE jos_vm_orders.order_status = 'C' GROUP BY...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.