Select Data Using LINQ to SQL Class in WPF

Introduction

This article shows how to select data using LINQ to SQL in WPF applications.

Create a WPF application as in Figure 1.


Figure 1: Create WPF application

Add LINQ to SQL classes to the project as in Figures 2 and 3.

 
Figure 2: Add LINQ to SQL

 
Figure 3: Choose database objects

MainWindow.xaml

  1. <Window x:Class="SelectData_WPF_LINQtoSQL.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow"  
  5.         Width="525"  
  6.         Height="350"  
  7.         Loaded="Window_Loaded">  
  8.     <Grid>  
  9.         <DataGrid x:Name="dgEmployee"  
  10.                   Width="267"  
  11.                   Margin="118,47,0,0"  
  12.                   HorizontalAlignment="Left"  
  13.                   VerticalAlignment="Top"  
  14.                   AutoGenerateColumns="False"  
  15.                   CanUserAddRows="False"  
  16.                   ColumnWidth="*">  
  17.             <DataGrid.Columns>  
  18.                 <DataGridTextColumn x:Name="dgrEmpId"  
  19.                                     Binding="{Binding EmpId}"  
  20.                                     Header="EmpId"  
  21.                                     IsReadOnly="True" />  
  22.                 <DataGridTextColumn x:Name="dgrFirstName"  
  23.                                     Binding="{Binding FirstName}"  
  24.                                     Header="FirstName"  
  25.                                     IsReadOnly="True" />  
  26.                 <DataGridTextColumn x:Name="dgrLastName"  
  27.                                     Binding="{Binding LastName}"  
  28.                                     Header="LastName"  
  29.                                     IsReadOnly="True" />  
  30.             </DataGrid.Columns>  
  31.         </DataGrid>  
  32.     </Grid>  
  33. </Window>  

MainWindow.xaml.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15.   
  16. namespace SelectData_WPF_LINQtoSQL  
  17. {  
  18.     /// <summary>  
  19.     /// Interaction logic for MainWindow.xaml  
  20.     /// </summary>  
  21.     public partial class MainWindow : Window  
  22.     {  
  23.         public MainWindow()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  29.         {  
  30.             dgEmployee.ItemsSource = objContext.Employees.ToList();  
  31.         }  
  32.   
  33.         DataClasses1DataContext objContext = new DataClasses1DataContext();  
  34.     }  
  35. }  

The output of the application is as in Figure 4.

 
Figure 4: Output of the application

Summary

In this article we saw how to select data using LINQ to SQL in WPF applications.


MVC Corporation
MVC Corporation is consulting and IT services based company.