In previous article, I have mentioned about Typescript Interview questions with Answers and AWS Lambda Interview Questions and Answers but now in this article, I have mentioned about Linq interview questions and answers which a C# or .NET developer must know.

LINQ-interview-questions-and-answers-min.jpg

1. What do you understand by LINQ?

LINQ stands for Language Integrated Query. LINQ allows us to write queries over local collection objects and remote data sources like SQL, XML documents etc.

It is a collection of standard query operators that adds native data querying facilities to .NET framework Languages like C#, VB.NET.

LINQ was designed by Anders Hejlsberg and was introduces in Visual Studio in 2008.

2. What are advantages of Linq?

Here are the advantages of Linq

  1. It is quick turnaround for development.
  2. It is helps us to access any type of resource.
  3. It is a cleaner and type-safety.
  4. Data is easy to setup and use.
  5. Tables are automatically mapped to class.
  6. We can debug query using Visual Studio while using Linq

3. What is Sequence and Query Operators in LINQ?

Sequence is a collection class on which you like to query. This collection class must implements the IEnumerable interface. An Element is a one item in the sequence.

Query operators takes the sequence, process it and returns the new sequence, for example, Skip, TakeWhile, SelectMany etc.

4. What are Extension Methods in Linq?

Extension methods in C# enable the addition of new methods to a pre-existing type, without modifying the original source code for that type.

Extension methods can be very useful for adding functionality to classes or interfaces found in a third-party library, or even to classes in the .NET Framework libraries.

Here is an example of string Extension method

public static class StringMethods
{
    public static bool IsStartWithLetterA(this string s)
    {
        return s.StartsWith("A");
    }
}
class Program
{
    static void Main(string[] args)
    {
        string value = "Aron";
        Console.WriteLine(value.IsStartWithLetterA()); //print true;
    }
}

5. What are Anonymous Types?

Anonymous types allow us to create new types without defining them. The "type" of the type is decided by the complier at run-time.

When we create a anonymous type we do not specify a name. We just write properties names and their values.

Example:

var k = new { FirstProperty = "Hello", SecondProperty = "World" };
Console.WriteLine(k.FirstProperty);

6. What is Anonymous method?

An anonymous method is a method without a name.

We just define their parameters and define the code into the curly braces.

Example:

delegate int func(int a, int b);
static void Main(string[] args)
{
    func f1 = delegate(int a, int b)
    {
        return a + b;
    };
 
    Console.WriteLine(f1(1, 2));
}

7. Explain what is LINQ to Objects?

When LINQ queries any IEnumerable(<T>) collection or IEnumerable directly without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML is referred as LINQ to Objects.

8. Explain what is the purpose of LINQ providers in LINQ?

A linq provider is software that implements the IQueryProvider and IQueryable interfaces for a particular data store. In other words, it allows you to write Linq queries against that data store.

For example, the Linq to XML provider allows you to write Linq queries against XML documents.

9.What are three main components of LINQ?

Three main components of LINQ are

  • Standard Query Operators
  • Language Extensions
  • LINQ Providers

10 What is the role of DataContext classes in LINQ?

The DataContext class is a LINQ to SQL class that acts as a conduit between a SQL Server database and the LINQ to SQL entity classes mapped to that database. The DataContext class contains the connection string information and the methods for connecting to a database and manipulating the data in the database.

By default, the DataContext class contains several methods that you can call, such as the SubmitChanges method that sends updated data from LINQ to SQL classes to the database.

11. How is LINQ useful than Stored Procedures?

Here are the benefits of using Linq over stored procedures:

  • Deployment: In LINQ, deployment becomes easier as everything is compiled into a single DLL. However, in Stored Procedures, an additional script has to be provided.
  • Debugging support: I can use any .NET debugger to debug the Linq queries. With Stored procedures, you cannot easily debug the SQL and that experience is largely tied to your database vendor like SQL Server.
  • Type safety
  • Easy to use: You don't have to learn T-SQL to do data access, nor do you have to learn the data access API (e.g. ADO.NET) necessary for calling the sprocs.

12. What is Difference between Select() and SelectMany() in LINQ?

In LINQ, Select() and SelectMany() are projection operators. The use of a Select() operator is to select a value from a collection whereas the use of SelectMany() operator is to select values from a group of collection, i.e. a nested collection.

Select many is like cross join operation in SQL where it takes the cross product.

using System;
using System.Linq;
using System.Collections.Generic;

namespace RegexExamples
{
    public class Program
    {
        public static void Main(string[] args)
        {
			List<string> animals = new List<string>() { "cat", "dog", "donkey" };
			List<int> number = new List<int>() { 10, 20 };

			var mix = number.SelectMany(num => animals, (n, a) => new { n, a });
 
			foreach(var val in mix)
			{
				Console.WriteLine(val);
			}

        }
    }
}

Output:

{ n = 10, a = cat }
{ n = 10, a = dog }
{ n = 10, a = donkey }
{ n = 20, a = cat }
{ n = 20, a = dog }
{ n = 20, a = donkey }

You may also like to read:

Entity Framework Core Interview Questions with Answers.

OOPS interview questions in C# (With Answers)

DevOps interview questions and answers

ASP.NET MVC Interview Questions and Answers