LINQ in C# — Interview Guide (With Examples)
LINQ (Language INtegrated Query) is a feature in C# that allows you to query collections, databases, XML, APIs, etc. using SQL-like syntax directly inside code.
Instead of writing loops, you can write clean, readable queries.
1. Why LINQ? (Interview Answer)
Before LINQ:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
List<int> evenNumbers = new List<int>();
foreach (var num in numbers)
{
if (num % 2 == 0)
{
evenNumbers.Add(num);
}
}
Using LINQ:
var evenNumbers = numbers.Where(x => x % 2 == 0).ToList();
✅ Cleaner
✅ Shorter
✅ More readable
2. Two Types of LINQ Syntax (Important Interview Question) ⭐
1. Method Syntax (Most Used)
var result = numbers.Where(x => x > 2);
2. Query Syntax (SQL Style)
var result = from n in numbers
where n > 2
select n;
👉 Both produce the same result
Interview Tip:
Method syntax is more commonly used in real projects
3. Basic LINQ Example
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var result = numbers
.Where(x => x > 2)
.Select(x => x * 2);
foreach (var item in result)
{
Console.WriteLine(item);
}
Output:
6
8
10
4. Common LINQ Methods (Very Important for Interviews)
| Method | Purpose |
|---|---|
| Where | Filter data |
| Select | Transform data |
| OrderBy | Sort ascending |
| OrderByDescending | Sort descending |
| First / FirstOrDefault | Get first element |
| Single / SingleOrDefault | Get one element |
| Any | Check if exists |
| All | Check condition for all |
| Count | Count items |
| GroupBy | Group data |
| Join | Join collections |
5. Where Example
var even = numbers.Where(x => x % 2 == 0);
6. Select Example
var squares = numbers.Select(x => x * x);
7. OrderBy Example
var sorted = numbers.OrderBy(x => x);
Descending:
var sorted = numbers.OrderByDescending(x => x);
8. First vs FirstOrDefault (Interview Favorite) 🎯
First
Throws exception if no data
var result = numbers.First(x => x > 10);
FirstOrDefault
Returns default value if no data
var result = numbers.FirstOrDefault(x => x > 10);
Default values:
-
int → 0
-
string → null
-
object → null
9. Single vs First (Important Interview Question)
Single
-
Expects only one result
-
Throws error if multiple
var user = users.Single(x => x.Id == 1);
First
-
Returns first match
var user = users.First(x => x.Id == 1);
10. Any Example
bool exists = numbers.Any(x => x > 3);
Output:
true
11. Count Example
int count = numbers.Count(x => x > 2);
12. GroupBy Example (Important)
var grouped = numbers.GroupBy(x => x % 2);
foreach (var group in grouped)
{
Console.WriteLine(group.Key);
foreach (var item in group)
{
Console.WriteLine(item);
}
}
Output:
0 (even)
2
4
1 (odd)
1
3
5
13. LINQ with Objects Example
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "John", Salary = 5000 },
new Employee { Id = 2, Name = "Sam", Salary = 7000 },
new Employee { Id = 3, Name = "David", Salary = 4000 }
};
var highSalary = employees
.Where(x => x.Salary > 5000)
.Select(x => x.Name);
Output:
Sam
14. Deferred Execution (Very Important Interview Question) ⚡
LINQ queries don't execute immediately.
They execute only when iterated.
Example:
var result = numbers.Where(x => x > 2);
numbers.Add(10);
foreach(var item in result)
{
Console.WriteLine(item);
}
Output:
3
4
5
10
Because LINQ executed after adding 10.
To execute immediately:
var result = numbers.Where(x => x > 2).ToList();
15. IEnumerable vs IQueryable (Advanced Interview Question)
| IEnumerable | IQueryable |
|---|---|
| Works in memory | Works in database |
| Slower for large data | Faster for large data |
| Used for collections | Used for EF Core |
Example:
IEnumerable<Employee> data
IQueryable<Employee> data
16. Select vs SelectMany (Interview Question)
Select
Returns nested collection
SelectMany
Flattens collection
Example:
var result = list.SelectMany(x => x.Items);
17. Join Example (Important)
var result = from emp in employees
join dept in departments
on emp.DeptId equals dept.Id
select new
{
emp.Name,
dept.DeptName
};
18. LINQ Advantages
✅ Readable
✅ Less code
✅ Strongly typed
✅ IntelliSense support
✅ Compile-time checking
19. Common Interview Questions
What is LINQ?
LINQ is used to query collections using SQL-like syntax in C#.
Types of LINQ
-
LINQ to Objects
-
LINQ to SQL
-
LINQ to XML
-
LINQ to Entities
When to use LINQ?
-
Filtering
-
Sorting
-
Grouping
-
Joining
20. Real-World Example (Very Useful)
You might use LINQ like this:
var shortlistedCandidates = candidates
.Where(x => x.Status == "Shortlisted")
.OrderByDescending(x => x.Experience)
.Take(10);
Perfect for:
-
Filtering candidates
-
Sorting by experience
-
Limiting results
21. Interview Tips
✔ Prefer method syntax
✔ Use FirstOrDefault safely
✔ Understand deferred execution
✔ Know IEnumerable vs IQueryable
✔ Practice GroupBy and Join
