Get last record from Database using LINQ in WPF

November 02, 2015 0 Comments

Today, I faced a problem which is related to banking application like account number. When I load the application to create new account for new customer then account number automatically increased by one from previously stored account. So, I want to pick last field value of Database table.

Get last record from Database using LINQ in WPF

Let's see the example:

     <Button Content="Button" HorizontalAlignment="Left" Margin="70,79,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>  

Code behind code

   private void Button_Click(object sender, RoutedEventArgs e)  
       DatabaseEntities dbe = new DatabaseEntities();  
       var getlastrec = dbe.users.ToArray().LastOrDefault();  

Here DatabaseEntities is the context class. In this context class, we have public properties that is users. Through this we can get all records from user table, which is exist in model folder. Before doing this code, must to add EDMX file

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google