0
0
C Sharp (C#)programming~20 mins

Join and GroupJoin operations in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Join and GroupJoin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple Join operation

What is the output of the following C# code using Join?

C Sharp (C#)
var numbers = new[] {1, 2, 3};
var words = new[] {"one", "two", "three"};

var query = numbers.Join(words,
    n => n,
    w => w.Length,
    (n, w) => $"{n}:{w}");

foreach (var item in query)
    Console.WriteLine(item);
A
1:one
2:two
B
1:one
2:two
3:three
C
2:two
3:three
D
1:one
3:three
Attempts:
2 left
💡 Hint

Check the length of each word and see which numbers match those lengths.

Predict Output
intermediate
2:00remaining
Output of a GroupJoin operation

What is the output of this C# code using GroupJoin?

C Sharp (C#)
var categories = new[] {
    new { Id = 1, Name = "Fruits" },
    new { Id = 2, Name = "Vegetables" }
};

var products = new[] {
    new { Name = "Apple", CategoryId = 1 },
    new { Name = "Carrot", CategoryId = 2 },
    new { Name = "Banana", CategoryId = 1 }
};

var query = categories.GroupJoin(products,
    c => c.Id,
    p => p.CategoryId,
    (c, ps) => new { Category = c.Name, Items = ps.Select(p => p.Name) });

foreach (var group in query)
{
    Console.WriteLine(group.Category + ": " + string.Join(", ", group.Items));
}
A
Fruits: Apple, Banana
Vegetables: Carrot
B
Fruits: Apple
Vegetables: Carrot, Banana
C
Fruits: Carrot, Banana
Vegetables: Apple
D
Fruits: 
Vegetables: Apple, Carrot, Banana
Attempts:
2 left
💡 Hint

GroupJoin groups products by their category IDs.

🧠 Conceptual
advanced
1:30remaining
Understanding Join vs GroupJoin

Which statement best describes the difference between Join and GroupJoin in C# LINQ?

AJoin returns elements without matching pairs, GroupJoin returns only matching pairs.
BJoin returns pairs of matching elements, while GroupJoin returns each element with a collection of matching elements.
CJoin groups elements by key, GroupJoin flattens all elements into a single list.
DJoin works only on arrays, GroupJoin works only on lists.
Attempts:
2 left
💡 Hint

Think about how many results each operation produces per element.

Predict Output
advanced
2:00remaining
Output of nested GroupJoin with empty groups

What is the output of this C# code using GroupJoin when some groups have no matches?

C Sharp (C#)
var departments = new[] {
    new { Id = 1, Name = "HR" },
    new { Id = 2, Name = "IT" },
    new { Id = 3, Name = "Sales" }
};

var employees = new[] {
    new { Name = "Alice", DeptId = 1 },
    new { Name = "Bob", DeptId = 2 }
};

var query = departments.GroupJoin(employees,
    d => d.Id,
    e => e.DeptId,
    (d, emps) => new { Dept = d.Name, Staff = emps.Select(e => e.Name) });

foreach (var group in query)
{
    Console.WriteLine(group.Dept + ": " + (group.Staff.Any() ? string.Join(", ", group.Staff) : "No employees"));
}
A
HR: No employees
IT: No employees
Sales: No employees
B
HR: Alice
IT: Bob
Sales: 
C
HR: 
IT: 
Sales: No employees
D
HR: Alice
IT: Bob
Sales: No employees
Attempts:
2 left
💡 Hint

Check how GroupJoin handles groups with no matching elements.

Predict Output
expert
3:00remaining
Complex Join with multiple keys and projection

What is the output of this C# code using Join with composite keys and projection?

C Sharp (C#)
var orders = new[] {
    new { OrderId = 1, CustomerId = 1, ProductId = 1 },
    new { OrderId = 2, CustomerId = 1, ProductId = 2 },
    new { OrderId = 3, CustomerId = 2, ProductId = 1 }
};

var customers = new[] {
    new { CustomerId = 1, Name = "Alice" },
    new { CustomerId = 2, Name = "Bob" }
};

var products = new[] {
    new { ProductId = 1, Name = "Book" },
    new { ProductId = 2, Name = "Pen" }
};

var query = orders.Join(customers,
    o => o.CustomerId,
    c => c.CustomerId,
    (o, c) => new { o.OrderId, c.Name, o.ProductId })
    .Join(products,
    oc => oc.ProductId,
    p => p.ProductId,
    (oc, p) => $"Order {oc.OrderId}: {oc.Name} bought {p.Name}");

foreach (var item in query)
    Console.WriteLine(item);
A
Order 1: Alice bought Pen
Order 2: Bob bought Book
Order 3: Alice bought Book
B
Order 1: Bob bought Book
Order 2: Alice bought Pen
Order 3: Alice bought Book
C
Order 1: Alice bought Book
Order 2: Alice bought Pen
Order 3: Bob bought Book
D
Order 1: Alice bought Book
Order 2: Bob bought Pen
Order 3: Bob bought Pen
Attempts:
2 left
💡 Hint

Trace the joins step by step matching CustomerId and ProductId.