LINQ is one of those C# features that can look strange when you first see it.
Something like this:
var transactions = await _context.Transactions
.Where(t => t.Amount > 100)
.OrderByDescending(t => t.Date)
.ToListAsync();
If I try to read all of that as syntax, it feels harder than it really is.
So I started reading it like instructions:
Go to Transactions
↓
Keep transactions over $100
↓
Sort newest first
↓
Turn the result into a list
Suddenly LINQ stopped looking so scary.
What Is LINQ?
LINQ stands for:
Language Integrated Query
It lets us query data using C#.
That data might come from:
- a
List<T> - an array
- EF Core
- XML
- other data sources
A simple list example:
var amounts = new List<decimal>
{
20,
150,
75,
300
};
Suppose I only want numbers greater than 100.
var largeAmounts = amounts
.Where(amount => amount > 100)
.ToList();
Result:
150
300
What Does Where() Mean?
Where() filters data.
.Where(t => t.Amount > 100)
I read that as:
Keep the transactions where the amount is greater than 100.
This part:
t => t.Amount > 100
is a lambda expression.
At first, the arrow made it look complicated.
But here, t just represents one item.
For each transaction t:
Is t.Amount > 100?
Yes → keep it
No → skip it
So:
.Where(t => t.Amount > 100)
can almost be read as English.
OrderBy() and OrderByDescending()
Suppose I want transactions from oldest to newest:
.OrderBy(t => t.Date)
Newest to oldest:
.OrderByDescending(t => t.Date)
I remember it like this:
OrderBy
small → large
old → new
A → Z
OrderByDescending
large → small
new → old
Z → A
Select() Changes the Shape
Select() is really useful.
Suppose the database entity has ten properties, but I only need the account name.
var names = accounts
.Select(a => a.Name)
.ToList();
Now instead of returning whole Account objects, the result contains names.
Checking
Savings
Credit Card
You can also create a new shape:
var accountList = accounts
.Select(a => new
{
a.Id,
a.Name
})
.ToList();
I think of Select() as:
Choose what I want the result to look like.
FirstOrDefault()
Suppose I want one account:
var account = await _context.Accounts
.FirstOrDefaultAsync(a => a.Id == id);
That means:
Find the first account whose ID matches this ID. If there isn’t one, return the default value.
For a class, that default is normally null.
So I need to handle that:
if (account == null)
{
return NotFound();
}
Any()
Any() answers a yes/no question.
For example:
bool hasTransactions =
await _context.Transactions.AnyAsync();
Result:
true
or
false
We can also add a condition:
bool accountExists = await _context.Accounts
.AnyAsync(a => a.Name == model.Name);
That reads almost like:
Does any account have this name?
Count()
This one is straightforward:
var count = await _context.Transactions.CountAsync();
You can count matching records too:
var pendingCount = await _context.Transactions
.CountAsync(t => t.IsPending);
Combining LINQ Methods
This is where LINQ gets powerful.
var recentExpenses = await _context.Transactions
.Where(t => t.Amount < 0)
.OrderByDescending(t => t.Date)
.Take(10)
.ToListAsync();
Instead of reading it as one giant C# statement, I break it apart:
Transactions
↓
Where amount is negative
↓
Sort newest first
↓
Take 10
↓
Create list
That is much easier for me.
LINQ With EF Core
When LINQ is used with EF Core, something very useful happens.
This:
var transactions = await _context.Transactions
.Where(t => t.AccountId == accountId)
.ToListAsync();
can be translated by EF Core into SQL.
Conceptually:
C# LINQ
↓
EF Core
↓
SQL
↓
Database
↓
Results
I do not have to manually write SQL for every basic query.
But understanding SQL is still valuable because EF Core is eventually talking to a relational database.
Why ToListAsync()?
When working with EF Core, I will often see:
ToListAsync()
instead of:
ToList()
The async version allows the application to wait for database I/O without blocking the request thread unnecessarily.
That connects LINQ with another important C# topic: async and await.
What I Would Say in an Interview
If someone asked:
“What is LINQ?”
I would answer:
LINQ is a C# feature that provides a consistent way to query and transform data. I can use methods such as
Where,Select,OrderBy, andFirstOrDefaultwith collections, and EF Core can translate many LINQ queries into SQL for database operations.
My personal LINQ cheat sheet:
Where = filter
Select = shape
OrderBy = sort
Any = does one exist?
Count = how many?
FirstOrDefault = give me one
Take = limit results
Once I started translating LINQ into simple instructions, I stopped seeing a bunch of dots and arrows.
I started seeing the data flow.
