LINQ allows you to write structured, type-safe queries over local object collections and remote data sources. LINQ interview questions are short and allow the interviewer to quickly test the depth of your knowledge in LINQ and to a certain degree, extension methods.
Let’s review a bunch of small functions that show you a few LINQ operations.
The example below shows you how to use the Where and Select LINQ extensions:
public static void LinqQueryAndSelection()
{
List<string> fruits = new List<string>
{ "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
// find all fruits that have names of length 5 or less
IEnumerable<string> query1 = fruits.Where(fruit => fruit.Length < 6);
foreach (string fruit in query1)
Console.WriteLine(fruit);
// convert the names of all the fruits to uppercase
IEnumerable<string> query2 = fruits.Select(n => n.ToUpper());
foreach (string fruit in query2)
Console.WriteLine(fruit);
}
This next example shows you how to do partial selections:
public static void LinqPartialSelection()
{
List<string> fruits = new List<string>
{ "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
// find first 3 fruits
IEnumerable<string> query1 = fruits.Take(3);
foreach (string fruit in query1)
Console.WriteLine(fruit);
// skip the first 3 fruits
IEnumerable<string> query2 = fruits.Skip(3);
foreach (string fruit in query2)
Console.WriteLine(fruit);
// single element operations
string firstFruit = fruits.First(); // apple
string lastFruit = fruits.Last(); // strawberry
string thirdFruit = fruits.ElementAt(2); // banana
string fruitStratingWithM = fruits.First(f => f.StartsWith("m")); // mango
}
This last example shows you how to use LINQ to aggregate values:
public static void LinqAggregation()
{
int[] numbers = { 3, 4, 5, 6, 7, 8 };
// aggregation operations
int count = numbers.Count(); // 6
int min = numbers.Min(); // 3
int max = numbers.Max(); // 8
double average = numbers.Average(); // 5.5
// the operators above also take an optional predicate
int countEvensOnly = numbers.Count(n => n % 2 == 0);
// assume that values decay over period
// MAX and MIN functions allow the numbers to be transformed
// before the operation
int maxAfterMultiplicationByTwo = numbers.Max(n => n * 2); // 16
}
There is much more to LINQ than these starter examples. From an interviewing perspective you should learn in depth about the standard Query operations which can be broken down into several categories:
- Filtering operations: Where, Take, Skip, etc.
- Project operations: Select, Join, etc.
- Ordering operations: OrderBy, ThenBy, Reverse. etc.
- Grouping operations: GroupBy
- Set operations: Concat, Union, etc.
- Element operations: First, FirstOrDefault, Last, etc.
- Aggregation operations: Count, Min, Max, Sum, etc.
- Qualifier operations: Contains, Any, All, etc.
- Conversion operations: ToArray, ToList, Cast, etc.
Another important fact to note is that LINQ operations can be chained as shown in the example below.
public static void LinqChaining()
{
List<string> fruits = new List<string>
{ "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
// operator chaining
IEnumerable<string> query1 = fruits
.Where(f => f.Contains("a"))
.OrderBy(f => f.Length)
.Select(f => f.ToUpper());
foreach (string fruit in query1)
Console.WriteLine(fruit);
}
So helpful! Thank you :)
ReplyDeleteVery Helpful
ReplyDeleteVery helpful for beginner
ReplyDeletefine
ReplyDeleteGreat Article
ReplyDeleteC# Training
C# Online Training
C-Sharp Training
Dot Net Training in Chennai
.Net Online Training
ASP.NET Training
Online LINQ
Training
LINQ Training
I'm happy to read this article.Thanks for your information! Keep sharing..
ReplyDeleteerp in chennai | cloud erp software in chennai
This comment has been removed by the author.
ReplyDeleteThanks for the great post! Your QAs have been added to the www.fullstack.cafe portal and back-linked!
ReplyDeleteCheck more LINQ Interview Questions here https://devinterview.io/dev/linq-interview-questions
ReplyDeleteĐại lý vé máy bay Aivivu
ReplyDeletegia ve may bay di my
chuyến bay thẳng từ mỹ về việt nam
chuyến bay thương mại từ canada về việt nam
gia ve may bay vietjet tu han quoc ve viet nam
giá vé máy bay từ anh về việt nam
các chuyến bay từ châu âu về việt nam
vé máy bay từ đức về việt nam giá rẻ
vé máy bay giá rẻ từ nga về việt nam
vé máy bay từ Hà nội đi Los Angeles
cách ly khách sạn trọn gói
I sent your articles links to all my contacts and they all adore it including me.
ReplyDeleteapp designers
I must appreciate our good effort in this article post. You done really nice work and share with us. Keep it up. Now it's time to avail chauffeur service manchester for more information.
ReplyDelete