LINQ to SQL questions and answers

What is the DataContext class and how is it related to LINQ?

- When we add a LINQ to SQL Classes item to our application and open the O/R Designer, then a blank surface appears which is called DataContext class.
- The DataContext class is a LINQ to SQL class that is a bridge between a SQL Server database and the LINQ to SQL entity classes.
- DataContext class has all the information of the connection with the database and the methods to manipulate the data in database.
- It is configured with connection information provided by the first item that is dragged onto the design surface.

How does LINQ to SQL differ from Entity framework?

- LINQ to SQL is suggested to use for fast development using SQL Server. Entity Framework is for enterprise level application and supports SQL server and other databases too.
- LINQ to SQL classes maps one entity class with one database table. Entity Framework supports conceptual model which maps to storage model via mappings.
- One Entity Framework class can be mapped to multiple tables or one table can be mapped to multiple classes.
- LINQ is mainly targeted on rapid development whereas Entity Framework is for enterprise scenarios where the requirement is to create loosely coupled framework.

Give example of get data from a table in LINQ to SQL.

- We can fetch the data from database table by simply writing a LINQ query and then running that query to retrieve the data.
- LINQ to SQL will convert all the LINQ operation to the appropriate SQL operations that we are familiar with.
- In the following example, the Names of employees from Mumbai are retrieved and shown in the console window.
// EmployeeDataContenxt inherits from System.Data.Linq.DataContext.
EmployeeDataContenxt db = new EmployeeDataContenxt();

var EmployeeNames = from emp in db.Employee
where emp.City == "Mumbai"
select emp.EmpName;

foreach (var employee in EmployeeNames)
{
   Console.WriteLine(employee);
}

Give an example of Insert operation in LINQ to SQL.

- For insertion of data in database we need to give values of each field to the entity class’s object and then we have to use SubmitChanges method on the DataContext.
- In the following example, a new Employee and his details are inserted to the Employee table by using InsertOnSubmit.

For example
// EmployeeDataContenxt inherits from System.Data.Linq.DataContext.
EmployeeDataContenxt db = new EmployeeDataContenxt();

Employee emp = new Employee();
emp.EmpName = "Arpit Jain";
emp.City = "Mumbai";
emp.EmployeeID = "123";
emp.Phone = "555-555-5555";
db.Employee.InsertOnSubmit(emp); // The new Employee object is added in the object model.
// In LINQ to SQL, the details will not be inserted in the database until
// SubmitChanges is executed.
db.SubmitChanges();

How to Update data in LINQ to SQL? Give example.

- For Updation of data in database, first we need to get the item and update it in the object model. After we have changed the object, call SubmitChanges on the DataContext to update the database.
- In the following example, Employee with EmployeeID 123 is retrieved. Then the name of the Employee is changed from "Arpit Jain" to "Arpit B. Jain". Finally,SubmitChanges is called to update the changes in the database.

For example
EmployeeDataContenxt db = new EmployeeDataContenxt();

var empData = from emp in db.Employees
where emp.EmployeeID == 123
select emp;
empData.EmpName = "Arpit B. Jain";
db.SubmitChanges();

Give an example of Delete operation in LINQ to SQL.

- For Deletion of an item, remove the item from the collection to which it belongs, and then execute SubmitChanges on the DataContext to commit the deletion.
- In the following example, the employee who has EmployeeID of 981 is retrieved from the database. Then, after ensuring that theEmployee data was retrieved, DeleteOnSubmit is executed to remove that object from the collection.
- At last, SubmitChanges is executed to remove the row from the database.
EmployeeDataContenxt db = new EmployeeDataContenxt();
var deleteEmp = from emp in db.Employees
where emp.EmployeeID == 981
select emp;

if (deleteEmp.Count() > 0)
{
   db.Employees.DeleteOnSubmit(deleteEmp.First());
   db.SubmitChanges();
}

How to do a WHERE IN clause in LINQ to SQL?

- LINQ has "Contains" which is same as "IN" in SQL but the approach is different. An element isn't "in" a set, a set "contains" an element.

For example
int[] names = { “Arpit”, “Bob”, “Jack” };
var result = from emp in db.Employees
where names.Contains(emp.EmpId)
select emp

-It can also be written in simpler way with a lambda expression.
-int[] names = { “Arpit”, “Bob”, “Jack” };

var result = db.Employees.Where(emp => names.Contains(emp.EmpId));
20 Silverlight Interview Questions and Answers - Freshers, Experienced
Silverlight interview questions and answers for freshers and experienced, Silverlight online test, Silverlight jobs - Silverlight architecture, Difference between WPF and Silverlight, limitations of using external fonts in Silverlight, how to perform Event handling in silver light, What is Silverlight.js file?
Silverlight architecture
Silverlight architecture - Silver light is a plug-in used for different platforms and browsers. It is designed for offering media experiences...
Difference between WPF and Silverlight
Difference between WPF and Silverlight - In terms of features and functionality, Silver light is a sub set of Windows Presentation Foundation...
Post your comment