SolvedPrivate

Help writing a C# function which returns the ID of the last record in a table

Posted on 2014-04-19
14
69 Views
1 Endorsement
Last Modified: 2016-02-16
I need help writing a C# Function using LINQ that will return me the ID of the last record from a table named Books in an SQL Server Database named MyLibrary.
I have named DbContext in my application MyLibraryDbContext.

I am using Entity Framework 6 (LINQ to Entities). The data is already in my application and can be retrieved using MyLibraryDbContext.

The function I need will look like something as below:

public int GetIdOfLastRecord()
{
MyLibraryDbContext context = new MyLibraryDbContext();

int ID;

// Code here

return ID;
}

Thank you for your help.
1
Comment
Question by:FaheemAhmadGul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
14 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40010121
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28415797.html

ps.  That is your previous question.

edit, appended:
Exactly what do you mean by last?  Is the table sorted in the way you require?
0
 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 40010172
Thank you for your comment.
I think this question is slightly different from my previous question. In my previous question, I wanted my Function to take a string parameter, then check if a record exists in the Books table that matches the string passed to it and return the ID of the record if it exists.
In my current question, I just want the ID of the last record from this table named Books. This new function does not take any parameters and just returns the ID of the last record whatever it is.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40010173
I appreciate there is a difference.

Exactly what do you mean by last?  Is the table sorted in the way you require?
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 40010181
By last record I mean the most recently added record, that is the record with the highest ID. So if the table has just three records, the record with the ID =3 will be the last record. If you consider the table data shown in my previous question with a similar theme, the last record will be the record that has BookTitle Emma. I am supposing the data in the table is sorted based on ID field (record with ID = 1 being considered the first record).
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40010188
Something like this:
var CustID= BooksDBContext.Customers
                       .Where(p => p.max(ID))
                       .ID;
0
 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 40010194
I do not get any option like max after typing
   .Where(p => p.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40010291
Does this work
var CustID= BooksDBContext.Customers.ID.Max();
0
 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 40010330
This does not work either.
0
 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 40010332
I did not think it would be such a difficult task. I am increasing the points to 200 now.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40010359
I'm not having a good day, sorry.

Please try this - I was thinking too much in SQL in my first code.  (The following or similar will work when using a collection in C#, not to certain about LINQ to ENTITY though).

var CustID= BooksDBContext.Customers
                       .Where(p => p.ID.Max())
                       .ID;

If it doesn't work do you mean it doesn't compile or runs with an error or runs but not returns what you expect



ps.  I've done a google search and the following might be useful:
http://stackoverflow.com/questions/3299545/linq-to-entities-easy-to-find-max-value-in-sql-but-difficult-in-linq
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 200 total points
ID: 40010639
Hi FaheemAhmadGul;

You can place the following code snippet in to the function and it should return the value you are looking for.

MyLibraryDbContext context = new MyLibraryDbContext();

var maxID = (from b in context.Books
             orderby b.ID descending
             select b.ID).FirstOrDefault( );

Open in new window

0
 
LVL 1

Author Closing Comment

by:FaheemAhmadGul
ID: 40010679
This has worked perfectly. Thank you. I am very grateful. Regards, Faheem
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40010711
Not a problem Faheem, glad I was able to help.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40011106
Just don't forget the sorting may be very time consuming - it depends on how many records you are working with.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question