Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Select clause projection in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Select Clause Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this LINQ Select projection?
Consider the following C# code using LINQ to project a list of numbers to their squares. What will be printed to the console?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = {1, 2, 3, 4};
        var squares = numbers.Select(x => x * x);
        foreach(var sq in squares) {
            Console.Write(sq + " ");
        }
    }
}
A1 2 3 4
BSystem.Linq.Enumerable+WhereSelectArrayIterator`2[System.Int32,System.Int32]
C1 4 9 16
DCompilation error
Attempts:
2 left
💡 Hint
Remember that Select projects each element to a new value.
Predict Output
intermediate
2:00remaining
What does this Select projection produce?
Given this code snippet, what will be the output?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        string[] words = {"apple", "banana", "cherry"};
        var firstChars = words.Select(w => w[0]);
        foreach(var ch in firstChars) {
            Console.Write(ch + "-");
        }
    }
}
Aa-b-c-
Ba b c
CCompilation error
Dapple-banana-cherry-
Attempts:
2 left
💡 Hint
Select projects each string to its first character.
Predict Output
advanced
2:00remaining
What is the output of this Select projection with anonymous types?
What will this program print?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        var people = new[] {
            new { Name = "Alice", Age = 30 },
            new { Name = "Bob", Age = 25 }
        };
        var names = people.Select(p => new { p.Name, IsAdult = p.Age >= 18 });
        foreach(var n in names) {
            Console.Write($"{n.Name}:{n.IsAdult} ");
        }
    }
}
AAlice:30 Bob:25
BAlice:True Bob:True
CCompilation error
DSystem.Object System.Object
Attempts:
2 left
💡 Hint
Select creates new anonymous objects with Name and IsAdult properties.
Predict Output
advanced
2:00remaining
What error does this Select projection cause?
What happens when you run this code?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        string[] words = {"one", null, "three"};
        var lengths = words.Select(w => w.Length);
        foreach(var len in lengths) {
            Console.Write(len + " ");
        }
    }
}
ASystem.NullReferenceException at runtime
B3 5
C3 0 5
DCompilation error
Attempts:
2 left
💡 Hint
One element is null, so accessing Length causes an error.
🧠 Conceptual
expert
2:00remaining
How many items are in the resulting sequence after this Select projection?
Given this code, how many elements will the 'result' sequence contain?
C Sharp (C#)
using System.Linq;

var numbers = new int[] { 2, 4, 6, 8 };
var result = numbers.Select(x => x / 2).Where(x => x > 2);
A4
B1
C3
D2
Attempts:
2 left
💡 Hint
First Select divides each number by 2, then Where filters those greater than 2.

Practice

(1/5)
1. What does the select clause do in a LINQ query in C#?
easy
A. It groups the data by a key.
B. It filters the data based on a condition.
C. It sorts the data in ascending order.
D. It chooses and shapes the data to return from the query.

Solution

  1. Step 1: Understand the role of select in LINQ

    The select clause defines what data to return from the query, shaping the output.
  2. Step 2: Differentiate from other clauses

    Filtering is done by where, sorting by orderby, grouping by group by. Only select shapes the output.
  3. Final Answer:

    It chooses and shapes the data to return from the query. -> Option D
  4. Quick Check:

    Select clause = shapes output [OK]
Hint: Select clause shapes output data, not filters or sorts [OK]
Common Mistakes:
  • Confusing select with where clause
  • Thinking select sorts data
  • Mixing select with group by
2. Which of the following is the correct syntax for a LINQ query using select to get only the names from a list of Person objects?
easy
A. var names = from p in people select p.Name;
B. var names = from p in people where p.Name;
C. var names = from p in people orderby p.Name select p;
D. var names = from p in people group p by p.Name;

Solution

  1. Step 1: Identify correct select syntax

    The correct syntax to project only the Name property is select p.Name.
  2. Step 2: Check other options for errors

    var names = from p in people where p.Name; uses where incorrectly without a condition. var names = from p in people orderby p.Name select p; selects whole object, not just names. var names = from p in people group p by p.Name; groups data, not selects.
  3. Final Answer:

    var names = from p in people select p.Name; -> Option A
  4. Quick Check:

    Select projects properties correctly [OK]
Hint: Select clause syntax: from x in y select x.Property [OK]
Common Mistakes:
  • Using where without condition
  • Selecting whole object instead of property
  • Confusing group by with select
3. What is the output of this code?
var numbers = new[] {1, 2, 3};
var result = from n in numbers select n * 2;
foreach(var r in result) Console.Write(r + " ");
medium
A. 1 2 3
B. 3 4 5
C. 2 4 6
D. Syntax error

Solution

  1. Step 1: Understand the select projection

    The query multiplies each number by 2, so 1 becomes 2, 2 becomes 4, 3 becomes 6.
  2. Step 2: Trace the output loop

    The foreach prints each doubled number followed by a space: "2 4 6 "
  3. Final Answer:

    2 4 6 -> Option C
  4. Quick Check:

    Select doubles numbers = 2 4 6 [OK]
Hint: Select transforms each item; multiply 1,2,3 by 2 = 2,4,6 [OK]
Common Mistakes:
  • Thinking select filters instead of transforms
  • Expecting original numbers
  • Confusing output format
4. Identify the error in this LINQ query:
var people = new List<Person>();
var names = from p in people select p.Name.ToUpper;
medium
A. select clause must come before from clause.
B. Missing parentheses after ToUpper method call.
C. List<Person> cannot be queried with LINQ.
D. Cannot select Name property directly.

Solution

  1. Step 1: Check method usage in select

    ToUpper is a method and requires parentheses: ToUpper().
  2. Step 2: Validate other options

    Cannot select Name property directly. is wrong because selecting Name is valid. List<Person> cannot be queried with LINQ. is wrong because List supports LINQ. select clause must come before from clause. is wrong because from must come first.
  3. Final Answer:

    Missing parentheses after ToUpper method call. -> Option B
  4. Quick Check:

    Method calls need () [OK]
Hint: Method calls need parentheses, e.g., ToUpper() [OK]
Common Mistakes:
  • Forgetting () after method names
  • Misordering LINQ clauses
  • Thinking List can't use LINQ
5. Given a list of products with properties Name and Price, how would you use a LINQ select clause to create a new list of anonymous objects containing only Name and a discounted price (10% off)?
hard
A. var discounted = from p in products select new { p.Name, DiscountedPrice = p.Price * 0.9 };
B. var discounted = from p in products select p.Name, p.Price * 0.9;
C. var discounted = from p in products where p.Price > 0.9 select p.Name;
D. var discounted = from p in products select new List { p.Name, p.Price * 0.9 };

Solution

  1. Step 1: Understand anonymous object creation in select

    To create new shaped data, use new { } with property assignments inside select.
  2. Step 2: Check each option's correctness

    var discounted = from p in products select new { p.Name, DiscountedPrice = p.Price * 0.9 }; correctly creates anonymous objects with Name and discounted price. var discounted = from p in products select p.Name, p.Price * 0.9; syntax is invalid (multiple values without new). var discounted = from p in products where p.Price > 0.9 select p.Name; filters but does not create discounted price. var discounted = from p in products select new List { p.Name, p.Price * 0.9 }; tries to create a List incorrectly.
  3. Final Answer:

    var discounted = from p in products select new { p.Name, DiscountedPrice = p.Price * 0.9 }; -> Option A
  4. Quick Check:

    Select new anonymous objects with discounted price [OK]
Hint: Use new { } in select to create shaped objects [OK]
Common Mistakes:
  • Trying to select multiple values without new
  • Using where instead of select for projection
  • Incorrect anonymous object syntax