Top LINQ Interview Questions and Answers – Part 1

Question 1. What is 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.

We can write LINQ query on any collection class which implements the IEnumerable interface.

Question 2. 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 e.g. Skip, TakeWhile, SelectMany etc.

Question 3. What are Extension Methods?

Extension methods are static functions of a static class. These methods can be invoked just like instance method syntax.

These methods are useful when we can not want to modify the class. Below is the example of Extension Method on string class.

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

        Console.ReadLine();
    }
}

Question 4. What are Anonymous Types?

Anonymous types are types that are generated by compiler at run time. When we create a anonymous type we do not specify a name. We just write properties names and their values. Compiler at runtime create these properties and assign values to them.

Below is the example of Anonymous type:

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

Anonymous class is useful in LINQ queries to save our intermediate results.

There are some restrictions on Anonymous types.

  • Anonymous types can not implement interfaces.
  • Anonymous types can not specify any methods.
  • We can not define static members.
  • All defined properties must be initialized.
  • We can only define public fields.

Question 5. What is Anonymous function?

An Anonymous function is a special function which does not have any name. We just define their parameters and define the code into the curly braces.

Below is the example of Anonymous function.

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));
}

Question 6. What is Lambda Expression?

Lambda Expression is a shortcut way to writing delegates. By using Lambda Expression we can write inline functions that can be used to pass as arguments to a function or returned as value from functions.

Lambda Expression has a special syntax. We specify input parameters at the left side, lambda operator in the middle and expression or statement block on the right side.

(input parameter1, input parameter2) => expression or statement block

Below is the example of Lambda expression:

delegate int func(int a, int b);
static void Main(string[] args)
{
    func f1 = (a, b) => a + b;

    Console.WriteLine(f1(1, 2));
}

Question 7. What is Action in LINQ?

Action are generic delegates provided by base class library of .NET. In Action delegate we can only store those methods that have only input parameters and void return types. We can specify upto 16 parameters.

Below is the example of Action delegate:

func f1 = (a, b) => a + b;

Action<int> printAction = (a) => Console.WriteLine(a);
printAction(f1(1, 3));

Question 8. What is Predicate delegate in LINQ?

Predicate is a delegated provided by base class library of NET. In Predicate delegate we can only store those method which have one input parameter and a bool return type.

Predicate delegates are mainly used in filtering scenarios in LINQ where we have to filter some list.

Below is the example of Predicate delegate:

Predicate<string> isStringStartWithMChar = (a) => a.StartsWith("m");
Console.WriteLine(isStringStartWithMChar("msmd"));

Question 9. What are Func delegates in LINQ?

Func is group of generic delegates. In generic parameters of Func delegate we specify the input parameters. The last parameter is always a return type.

Below is the example of Func delegate.

Func<string, bool> isStringStartWithMChar = (a) => a.StartsWith("m");
Func<string, string, bool> areBothStringsEqual = (a, b) => a == b;

Console.WriteLine(isStringStartWithMChar("msmd"));
Console.WriteLine(areBothStringsEqual("a", "b"));

Question 10. What is LINQ Deferred Execution?

In deferred execution, a LINQ query is not executed when we declare our query. Query is executed when query variable is executed in a loop like for, foreach.

Below is the example of deferred execution.

var query = from n in employees
            where n.Name.EndsWith("l")
            select n;
//Query is not executed yet

foreach(var emp in query)  //Query executed
{
    Console.WriteLine(emp.Name);
}