Join in LINQ — Complete Guide
Join in LINQ — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of LINQ Tutorial on Toolliyo Academy.
On this page
LINQ Tutorial · Lesson 33 of 100
Join
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Queries & joins · ~14 min read · Module 4: Grouping & Joining · ShopNest.Analytics
Introduction
You know LINQ basics now. Here we use Join in real app situations — filters, reports, and search. Still plain language, just a bit more depth. Join matches rows from two collections on equal keys — like orders.CustomerId equals customers.Id. You get combined data in one result. Reports need customer names next to order totals. Without Join you fetch orders, then loop customers — slow and messy. Join does it in one query.
Grouping is how dashboards show "sales per region" without copying data to Excel. Practice with small sample lists first.
When will you use this?
Use grouping and joins when a screen shows summaries — sales by city, orders per customer.
- Sales dashboards group orders by region. HR reports group employees by department.
- Join links orders to customers — the same pattern in billing, CRM, and inventory apps.
Real-world: Zoho-style HRMS
Real product: Zoho-style HRMS (HR software). HR managers rely on employee directory and payroll reports every day. On this product, developers use Join in LINQ to attach customer names to order rows for billing exports. Without it, the team would write longer loops, ship slower features, or pull too much data from SQL Server. The example below is simplified on purpose — production code adds error handling, logging, and tests around the same LINQ pattern.
Production-style code
// Billing export — orders with customer name
var rows = await (
from o in _context.Orders.AsNoTracking()
join c in _context.Customers on o.CustomerId equals c.Id
where o.OrderDate >= fromDate
orderby o.OrderDate descending
select new OrderExportDto
{
OrderId = o.Id,
CustomerName = c.Name,
City = c.City,
GrandTotal = o.GrandTotal
}).ToListAsync(ct);
What happens in production: In Zoho-style HRMS, getting Join in LINQ right means HR managers see correct employee directory and payroll reports quickly. That is the difference between a tutorial snippet and software people trust with money and operations data.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
var customers = new List<Customer>
{
new Customer { Id = 1, Name = "Rahul" },
new Customer { Id = 2, Name = "Priya" }
};
var orders = new List<Order>
{
new Order { Id = 10, CustomerId = 1, Total = 1500 },
new Order { Id = 11, CustomerId = 1, Total = 900 },
new Order { Id = 12, CustomerId = 2, Total = 2200 }
};
var report = from o in orders
join c in customers on o.CustomerId equals c.Id
select new { c.Name, o.Total, o.Id };
foreach (var row in report)
Console.WriteLine($"Order {row.Id}: {row.Name} paid ₹{row.Total}");
Line-by-line walkthrough
| Code | What it means |
|---|---|
var customers = new List<Customer> | Part of the Join example — read it together with the lines before and after. |
{ | Part of the Join example — read it together with the lines before and after. |
new Customer { Id = 1, Name = "Rahul" }, | Part of the Join example — read it together with the lines before and after. |
new Customer { Id = 2, Name = "Priya" } | Part of the Join example — read it together with the lines before and after. |
}; | Closes a block started by { or ( above. |
var orders = new List<Order> | Part of the Join example — read it together with the lines before and after. |
{ | Part of the Join example — read it together with the lines before and after. |
new Order { Id = 10, CustomerId = 1, Total = 1500 }, | Part of the Join example — read it together with the lines before and after. |
new Order { Id = 11, CustomerId = 1, Total = 900 }, | Part of the Join example — read it together with the lines before and after. |
new Order { Id = 12, CustomerId = 2, Total = 2200 } | Part of the Join example — read it together with the lines before and after. |
}; | Closes a block started by { or ( above. |
var report = from o in orders | Query syntax — reads like SQL: from items in collection where ... select ... |
join c in customers on o.CustomerId equals c.Id | Combines two collections on matching keys — orders with customers. |
select new { c.Name, o.Total, o.Id }; | Pick or shape the fields you want in the result (query syntax). |
How it works (big picture)
- join ...
- on o.CustomerId equals c.Id pairs each order with the matching customer.
- Inner join skips orders with no customer (orphan CustomerId).
- Query syntax reads like SQL; method syntax uses .Join().
Do this on your computer
- Type the in-memory join example and run it.
- Add an order with CustomerId = 99 — notice it disappears (inner join).
- Rewrite the same join using method syntax: orders.Join(customers, ...).
- Lesson 35 covers left join for customers with zero orders.
- Read the real-world section and name which part of the app uses this topic.
- Run the example in a console app or LINQPad and confirm the output.
- Change one filter or sort in the example and predict the result before you run it.
Experiments — try changing this
- Change a filter value (price, date, name) and run again — see how results change.
- Remove one operator from the chain, run, and read the error or different output.
- In EF Core, enable SQL logging and see what SQL Join generates.
Remember
Join combines two sequences on matching keys. Query syntax: join c in customers on o.CustomerId equals c.Id. EF Core translates to SQL INNER JOIN.
Common questions
How long should I spend on Join?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–45 minutes per new operator; fundamentals may take an afternoon.
What if I get stuck on Join?
Re-read the line-by-line walkthrough, check for typos in lambdas (=>), and compare your code character-by-character with the example. Search the exact exception message — someone else had it too.
Where is Join used in real jobs?
See the real-world section above — the same pattern appears in e-commerce, banking, HRMS, and SaaS reporting. Interviewers ask you to explain it with one concrete example.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!