How to write LINQ query for Inner Join with and condition?

Sometimes, you need to apply inner join with and condition. To write query for inner join with and condition you need to make two anonymous types (one for left table and one for right table) by using new keyword and compare both the anonymous types as shown below: DataContext context… Continue reading

What are different types of joins in LINQ?

LINQ has a JOIN query operator that provides SQL JOIN like behavior and syntax. There are four types of Joins in LINQ. INNER JOIN – Inner join returns only those records or rows that match or exists in both the tables. DataContext context = new DataContext(); var q = (from… Continue reading

What is explicit loading?

By default, related objects (child objects) are not loaded automatically with its parent object until they are requested. To do so you have to use the load method on the related entity’s navigation property. For example: // Load the products related to a given category context.Entry(cat).Reference(p => p.Product).Load(); If lazy… Continue reading

What is difference between lazy/deferred loading and eager loading?

Lazy/Deferred Loading: In case of lazy loading, related objects (child objects) are not loaded automatically with its parent object until they are requested. By default LINQ supports lazy loading. For Example: DataContext context = new DataContext(); var query = context.Department.Take(3); // Lazy loading foreach(var Dept in query) { Console.WriteLine(Dept.Name); foreach(var… Continue reading

What is difference between deferred execution and immediate execution?

Deferred Execution: In case of differed execution, a query is not executed at the point of its declaration. It is executed when the Query variable is iterated by using loop like as for, foreach. DataContext context = new DataContext(); var query = from customer in context.Customers where customer.City == “Delhi”… Continue reading

What is data context class? Explain its role?

Data context class is a LINQ to SQL class that acts as a medium between a SQL server database and LINQ to SQL entities classes mapped to that database. It has following responsibilities: Contains the connection string information, methods to connect to the database and manipulating the data in the… Continue reading

Explain Union, Intersect and Except?

Union – It returns the union of two collections. In union elements will be unique. int[] numbers1 = { 1, 2, 3, 4, 5 }; int[] numbers2 = { 5, 6, 7, 8, 9, 10 }; IEnumerable < int > union = numbers1.Union(numbers2); //1,2,3,4,5,6,7,8,9,10  Intersect – It returns the intersection… Continue reading

What is difference between into and let keyword in LINQ?

Into – This is used to store the results of a group, join or select clause into a temporary variable. It hides the previous range variable and create a temporary range variable which you can be used further. DataContext context = new DataContext(); var q = from emp in context.tblEmployee… Continue reading

What is difference among Any, All and Contains?

Any – It checks whether at least one element in a collection satisfy a specified condition. It returns true if at least one element satisfy the condition else returns false if they do not. All – It checks whether all the elements in a collection satisfy a specified condition. It… Continue reading