Top LINQ Interview Questions and Answers – Part 2

Question 11. What is the use of into keyword in LINQ?

‘into’ keyword lets us save the intermediate result into a variable. We can further write LINQ query against this variable.

static string[] names = { "Kapil Malhotra", "Michael Jackson", "Miami Sinha" };
static void Main(string[] args)
{
    var result = from name in names
                    where name.StartsWith("M")
                    select name into fNames
                    where fNames.EndsWith("n")
                    select fNames;
    foreach(var name in result) //only 1 result
    {
        Console.WriteLine(name); //Print Michael Jackson
    }
}

Question 12. What is the use of let keyword in LINQ?

‘let’ keyword is used for introducing a new sequence in addition to existing sequence. Sequence is a collection class on which you are writing the query.

Below is the example of ‘let’ keyword.

static string[] names = { "Kapil Malhotra", "Kavita Kumari", "Michael Jackson", "Miami Sinha" };
static void Main(string[] args)
{
    var result = from name in names
                    let nameWithoutAChar = name.Replace("a", "")
                    where nameWithoutAChar.Length > 4
                    select nameWithoutAChar;

    foreach (var name in result)
    {
        Console.WriteLine(name);
    }

    // Kpil Mlhotr
    // Kvit Kumri
    // Michel Jckson
    // Mimi Sinh
}

Question 13. What is the difference between IEnumerable and IQueryable interfaces?

LINQ has two types of data sources.

  1. Local data sources
  2. Remote data sources

Local data sources are collection classes that resides in local memory. Remote data sources is used when we are writing queries against data from db file.

Local object collection class must implement the IEnumerable<T> interface. Remote objects must implement the IQueryable<T> inteface.

IQueryable<T> interface extend the IEnumerable<T> interface with some new methods for fetching data from remote sources.

Question 14. What is the use of AsEnumerable() method?

When we are writing queries against remote data sources, they work on IQueryable<T> interface that means LINQ query and query operators converts into sql query and fetch data.

But when we used AsEnumerable() in the query, all subsequent query operators work against in memory collection. For example:

MyDbContext context = new MyDbContext();
var result = context.Employess.Where(w => w.Name.StartsWith("K")).AsEnumerable()
                     .Where(w => w.Name.EndsWith("M")).ToList();

In the above example first where query operator converts into sql query and execute on the database. After the AsEnumerable() method, the second where query operator work against in-memory collection class.

Question 15. Is DataContext class thread-safe?

No, DataContext class is not thread-safe. Create a new DataContext class for each thread.

Question 16. Can you give me some examples of filtering operators in LINQ?

Some filtering operators are Where, Distinct, Skip, Take, SkipWhile, and TakeWhile.

Question 17. Whare are the differences between Skip and SkipWhile operators?

SkipWhile takes a func delegate to skip the first matching records from the result.

Below is the example:

static string[] names = { "first", "first 1", "second", "first 2" };
static void Main(string[] args)
{
    var result = names.SkipWhile(w => w.StartsWith("f"));

    foreach (var name in result)
    {
        Console.WriteLine(name);
    }

    //second
    //first 2
}

Skip only takes number of items to skip. Below is the example:

static string[] names = { "first", "first 1", "second", "first 2" };
static void Main(string[] args)
{
    var result = names.Skip(1);

    foreach (var name in result)
    {
        Console.WriteLine(name);
    }

    //first 1
    //second
    //first 2
}

Question 18. How to write subqueries in LINQ? (Very important and most asked interview question)

Subqueries are useful when we have to fetch records from second table based on records of first table. 

Below is the example of writing subquries in LINQ:

var filterOrders = from cust in customers
                    where cust.Name.StartsWith("M")
                    select new
                    {
                        Name = cust.Name,
                        Orders = from order in orders
                                where order.CustomerId == cust.Id
                                select order
                    };

Question 19. When to use SelectMany operator in LINQ?

SelectMany is useful when we have to flattens the input sequence into one sequence.

Below is an example of SelectMany operator.

static string[] names = { "kapil malhotra", "ramesh mishra"};
static void Main(string[] args)
{
    var query = names.SelectMany(name => name.Split(' '));
    foreach(var item in query)
    {
        Console.WriteLine(item);
    }

    //kapil
    //malhotra
    //ramesh
    //mishra
}

Question 20. Can we implement joins in LINQ?

Yes, we can implement joins in LINQ. We can implement different types of joins like cross join, equi join, outer joins.

Below is the syntax of joins:

from outerItem in outerSequence
join innerItem in innerSequence
    on outerItem.property equals innerItem.property
select new { outerItem, innerItem }