How To Using LINQ to SQL Classes With Example

DataGridView,LINQ to SQL,DataGridView ,DataGridView's,NorthwindDataContext object,DataGridView's DataSource property,LINQ query,combo box
Share it:

Using LINQ to SQL Classes


Once the required LINQ to SQL classes has been successfully generated, we can now use them in our application. 

For our GUI, we will be using a DataGridView control to display the queried records. Head back to the Windows Forms Designer. Drag a DataGridView control from the Toolbox's Data category to the form. Set the DataGridView's Dock property to Fill so it will take up all the space of the form. That resize the form to a larger size so it will properly show all the records that we will query.

DataGridView


We will be using the following code:

using System;                                                          
using System.Linq;                                                     
using System.Windows.Forms;                                            
                                                                       
namespace LinqToSqlDemo                                                
{                                                                      
    public partial class Form1 : Form                                  
    {                                                                  
        public Form1()                                                 
        {                                                              
            InitializeComponent();                                     
        }                                                              
                                                                       
        private void Form1_Load(object sender, EventArgs e)            
        {                                                              
            NorthwindDataContext database = new NorthwindDataContext();
           
            var employees = from employee in database.Employees 
            select new 
            { 
            employee.EmployeeID, 
            employee.FirstName, 
            employee.LastName, 
            employee.BirthDate, 
            employee.Address, 
            employee.Country 
            }; 
           
            dataGridView1.DataSource = employees; 
        }                                                              
    }                                                                  
}

Double click the Form's title bar in the Designer to generate a handler for the form's Load event. Add the codes in lines 16-29. and the code at line 16 creates a new NorthwindDataContext object. This will be used to access the tables of the database and the rows each table contains. Lines 18-27 uses a LINQ query that accesses the NorthwindDataContext's Employees property containing each record for the employee. The select clause of the query only selects some of the properties of every employee. Line 29 uses the DataGridView's DataSource property and assigns the result of the query as it's the data source.


When you run the program, you will see all the records from the Employees table.



records from the Employees table

Lines 18-27 is a simple LINQ query that retrieves several properties of every employee in the Employees database. You can perform different LINQ queries that suit your needs. For example, we can modify the LINQ query in 18-27 to only show employees who live in the USA.
var employees = from employee in database.Employees
                where employee.Country == "USA"
                select new
                {
                    employee.EmployeeID,
                    employee.FirstName,
                    employee.LastName,
                    employee.BirthDate,
                    employee.Address,
                    employee.Country
                };

You can provide controls, for example, a combo box containing different countries, and modify the query based on the selected country in the combo box.
Share it:

How To Using LINQ to SQL Classes With Example

LINQ

Querying a Database with LINQ to SQL

Post A Comment:

0 comments: