Monday, 30 November 2015

LINQ to SQL Writing Select Query

MyDBDataContext sqlObj = new MyDBDataContext();
var employees = from emps in sqlObj.tblEmployees
                select new
                {
                    emps.EmployeeID,
                    emps.EmployeeName,
                    emps.Salary
                };
gvemployees.DataSource = employees;
gvemployees.DataBind();


LINQ Query to Insert

MyDBDataContext sqlObj = new MyDBDataContext();
tblDepartment dept = new tblDepartment();
dept.DepartmentName = "Deployment";
sqlObj.tblDepartments.InsertOnSubmit(dept);
sqlObj.SubmitChanges();

LINQ to SQL Update

MyDBDataContext sqlObj = new MyDBDataContext();
tblDepartment dept = sqlObj.tblDepartments.Single(x => x.DepartmentID == 5);
dept.DepartmentName = "Deployment Department";
sqlObj.SubmitChanges();


LINQ to SQL Delete

MyDBDataContext sqlObj = new MyDBDataContext();
tblDepartment dept = sqlObj.tblDepartments.Single(x => x.DepartmentID == 5);
sqlObj.tblDepartments.DeleteOnSubmit(dept);
sqlObj.SubmitChanges();

LINQ to SQL LIKE Operator

LINQ Query Contains():
MyDBDataContext sqlObj = new MyDBDataContext();
var employees = from emps in sqlObj.tblEmployees
                where emps.EmployeeName.Contains("en")
                select new
                {
                    emps.EmployeeID,
                    emps.EmployeeName,
                    emps.Salary
                };
gvemployees.DataSource = employees;
gvemployees.DataBind();

LINQ Query StartsWith() :
MyDBDataContext sqlObj = new MyDBDataContext();
var employees = from emps in sqlObj.tblEmployees
                where emps.EmployeeName.StartsWith("v")
                select new
                {
                    emps.EmployeeID,
                    emps.EmployeeName,
                    emps.Salary
                };
gvemployees.DataSource = employees;
gvemployees.DataBind();

LINQ Query EndsWith() :
MyDBDataContext sqlObj = new MyDBDataContext();
var employees = from emps in sqlObj.tblEmployees
                where emps.EmployeeName.EndsWith("ms")
                select new
                {
                    emps.EmployeeID,
                    emps.EmployeeName,
                    emps.Salary
                };
gvemployees.DataSource = employees;
gvemployees.DataBind();

No comments:

Post a Comment