Menu ▼

Previous Next

LINQ to SQL Writing Select Query :

When we created MyDBData.dbml it will automatically add the class MyDBDataContext. By creating the instance to the MyDBDataContext class we can access the tables and stored procedures.

Employees table data
employees

LINQ Query :
Below LINQ query will get the employee details from database table "tblEmployees"

MyDBDataContext sqlObj = new MyDBDataContext();

var employees = from emps in sqlObj.tblEmployees

                select new

                {

                    emps.EmployeeID,

                    emps.EmployeeName,

                    emps.Salary

                };

gvemployees.DataSource = employees;

gvemployees.DataBind();


Output :
select-emloyees

LINQ Query with where conditon and order by :
Below LINQ query will get the employee details from database table "tblEmployees" who are having Salary more than 5000.

MyDBDataContext sqlObj = new MyDBDataContext();

var employees = from emps in sqlObj.tblEmployees

                where emps.Salary > 5000

                orderby emps.EmployeeName

                select new

                {

                    emps.EmployeeID,

                    emps.EmployeeName,

                    emps.Salary

                };

gvemployees.DataSource = employees;

gvemployees.DataBind();


Output :
select-employees-where-condition
Previous Next




Search dotnetlearners.com