Search

Monday, November 2, 2015
0 comments

Get last record from Database using LINQ in WPF

1:40 AMMonday, November 2, 2015
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:

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

Code behind code

   private void Button_Click(object sender, RoutedEventArgs e)  
     {  
       DatabaseEntities dbe = new DatabaseEntities();  
       var getlastrec = dbe.users.ToArray().LastOrDefault();  
       MessageBox.Show(getlastrec.Id.ToString());  
     }  

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
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top