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

LINQ query syntax 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
🎖️
LINQ Query 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 query?

Consider the following C# code using LINQ query syntax. What will be printed?

C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5 };
        var query = from n in numbers
                    where n % 2 == 0
                    select n * n;
        foreach (var item in query) {
            Console.Write(item + " ");
        }
    }
}
A2 4
B1 9 25
C4 16
D1 4 9 16 25
Attempts:
2 left
💡 Hint

Look at the where clause filtering even numbers, then the select projects squares.

🧠 Conceptual
intermediate
2:00remaining
Which LINQ query syntax correctly selects names starting with 'J'?

Given a list of names, which LINQ query syntax correctly selects only those starting with the letter 'J'?

C Sharp (C#)
string[] names = { "John", "Alice", "James", "Bob" };
Avar result = from name in names select name where name.StartsWith("J");
Bvar result = from name in names where name.StartsWith("J") select name;
Cvar result = from name in names select name if name.StartsWith("J");
Dvar result = from name in names where name.Contains("J") select name;
Attempts:
2 left
💡 Hint

Remember the where clause comes before select in query syntax.

🔧 Debug
advanced
2:00remaining
What error does this LINQ query produce?

Examine the following LINQ query code. What error will it cause when compiled?

C Sharp (C#)
int[] nums = {1, 2, 3};
var q = from n in nums
        where n > 1
        select n > 2 ? n : ;
ASyntaxError: incomplete ternary operator
BTypeError: cannot convert bool to int
CRuntime NullReferenceException
DNo error, outputs 3
Attempts:
2 left
💡 Hint

Look carefully at the ternary operator syntax in the select clause.

Predict Output
advanced
2:00remaining
What is the output of this LINQ query with ordering?

What will be printed by this C# program using LINQ query syntax with ordering?

C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        string[] fruits = { "apple", "banana", "cherry", "date" };
        var query = from f in fruits
                    orderby f.Length descending, f
                    select f;
        foreach (var fruit in query) {
            Console.Write(fruit + " ");
        }
    }
}
Adate apple banana cherry
Bbanana cherry date apple
Ccherry banana apple date
Dbanana cherry apple date
Attempts:
2 left
💡 Hint

Order by length descending, then alphabetically ascending.

🧠 Conceptual
expert
2:00remaining
How many items are in the resulting collection?

Given this LINQ query, how many items will the result collection contain?

C Sharp (C#)
int[] values = { 2, 4, 6, 8, 10 };
var result = from v in values
             where v % 4 == 0
             select v / 2;
A2
B3
C4
D5
Attempts:
2 left
💡 Hint

Check which numbers in the array are divisible by 4.

Practice

(1/5)
1. What does the LINQ query syntax in C# primarily help you do with collections?
easy
A. Write low-level assembly code
B. Create new classes and objects
C. Manage memory allocation manually
D. Filter, sort, group, and select data in a readable way

Solution

  1. Step 1: Understand LINQ purpose

    LINQ query syntax is designed to work with collections to filter, sort, group, and select data.
  2. Step 2: Compare options

    Options B, C, and D describe unrelated programming tasks, not LINQ's purpose.
  3. Final Answer:

    Filter, sort, group, and select data in a readable way -> Option D
  4. Quick Check:

    LINQ = Data querying [OK]
Hint: LINQ reads like English to query collections [OK]
Common Mistakes:
  • Confusing LINQ with object creation
  • Thinking LINQ manages memory
  • Assuming LINQ writes low-level code
2. Which of the following is the correct basic syntax to start a LINQ query in C#?
easy
A. var result = select item from collection;
B. var result = collection from item select;
C. var result = from item in collection select item;
D. var result = item select from collection;

Solution

  1. Step 1: Recall LINQ query syntax structure

    The correct LINQ query starts with 'from', then the range variable, then 'in' and the collection, followed by 'select'.
  2. Step 2: Match options to syntax

    Only var result = from item in collection select item; follows this correct order: 'from item in collection select item;'. Others have wrong order or keywords.
  3. Final Answer:

    var result = from item in collection select item; -> Option C
  4. Quick Check:

    from ... in ... select ... [OK]
Hint: LINQ starts with 'from' then 'in' then 'select' [OK]
Common Mistakes:
  • Swapping 'from' and 'select' keywords
  • Omitting 'in' keyword
  • Incorrect order of clauses
3. What will be the output of the following C# code?
int[] numbers = {1, 2, 3, 4, 5};
var evens = from n in numbers
            where n % 2 == 0
            select n;
foreach(var num in evens) {
    Console.Write(num + " ");
}
medium
A. 1 3 5
B. 2 4
C. 1 2 3 4 5
D. No output

Solution

  1. Step 1: Analyze the LINQ query filtering condition

    The query selects numbers where n % 2 == 0, meaning even numbers only.
  2. Step 2: Identify even numbers in the array

    From {1,2,3,4,5}, even numbers are 2 and 4.
  3. Final Answer:

    2 4 -> Option B
  4. Quick Check:

    Filter evens = 2 4 [OK]
Hint: Look for 'where' condition filtering evens [OK]
Common Mistakes:
  • Selecting odd numbers by mistake
  • Ignoring the 'where' clause
  • Assuming all numbers are selected
4. Identify the error in the following LINQ query syntax:
var result = from x collection
             where x > 10
             select x;
medium
A. Missing 'in' keyword between 'x' and 'collection'
B. Incorrect 'where' clause syntax
C. 'select' keyword should come before 'where'
D. Variable 'x' is not declared

Solution

  1. Step 1: Check the 'from' clause syntax

    The 'from' clause must have 'from variable in collection'. Here 'in' is missing.
  2. Step 2: Verify other clauses

    The 'where' and 'select' clauses are correctly placed and syntactically valid.
  3. Final Answer:

    Missing 'in' keyword between 'x' and 'collection' -> Option A
  4. Quick Check:

    'from x in collection' needed [OK]
Hint: Check 'from' clause for 'in' keyword [OK]
Common Mistakes:
  • Omitting 'in' keyword in 'from' clause
  • Misordering 'where' and 'select'
  • Assuming variable declaration needed
5. Given a list of strings words, which LINQ query syntax correctly selects words starting with 'a' and orders them alphabetically?
hard
A. var query = from w in words where w.StartsWith("a") orderby w select w;
B. var query = from w in words orderby w where w.StartsWith("a") select w;
C. var query = from w in words select w where w.StartsWith("a") orderby w;
D. var query = from w in words select w orderby w where w.StartsWith("a");

Solution

  1. Step 1: Understand LINQ clause order

    The correct order is 'from', then 'where', then 'orderby', then 'select'. var query = from w in words where w.StartsWith("a") orderby w select w; follows this order.
  2. Step 2: Verify filtering and ordering

    var query = from w in words where w.StartsWith("a") orderby w select w; filters words starting with 'a' and orders them alphabetically before selecting.
  3. Final Answer:

    var query = from w in words where w.StartsWith("a") orderby w select w; -> Option A
  4. Quick Check:

    where before orderby in LINQ [OK]
Hint: Remember clause order: from, where, orderby, select [OK]
Common Mistakes:
  • Placing 'orderby' before 'where'
  • Misplacing 'select' clause
  • Using wrong method syntax inside query