Introduction
LINQ method syntax helps you easily work with collections like lists or arrays to find, filter, or change data.
Jump into concepts and practice - no test required
LINQ method syntax helps you easily work with collections like lists or arrays to find, filter, or change data.
collection.MethodName(arguments)
var evens = numbers.Where(n => n % 2 == 0);
var namesSorted = names.OrderBy(name => name);
var squares = numbers.Select(n => n * n);
This program uses LINQ method syntax to find even numbers, get squares, and count even numbers in an array.
using System; using System.Linq; class Program { static void Main() { int[] numbers = { 1, 2, 3, 4, 5, 6 }; var evenNumbers = numbers.Where(n => n % 2 == 0); var squares = numbers.Select(n => n * n); var countEven = numbers.Count(n => n % 2 == 0); Console.WriteLine("Even numbers: " + string.Join(", ", evenNumbers)); Console.WriteLine("Squares: " + string.Join(", ", squares)); Console.WriteLine("Count of even numbers: " + countEven); } }
LINQ methods do not change the original collection; they create new results.
Use lambda expressions (like n => n % 2 == 0) to specify conditions or transformations.
LINQ method syntax lets you work with collections easily using chainable methods.
Common methods include Where (filter), Select (transform), OrderBy (sort), and Count (count items).
It helps write clear and readable code for data operations.
Where do in method syntax?WhereWhere method is used to select only those items from a collection that satisfy a given condition.Select which transforms items, or OrderBy which sorts, Where filters items based on a predicate.Where = Filter [OK]numbers using LINQ method syntax?Where.n => n % 2 == 0 correctly tests if a number is even.Where with condition [OK]var numbers = new List<int> {1, 2, 3, 4, 5};
var result = numbers.Where(n => n > 3).Select(n => n * 2).ToList();
Console.WriteLine(string.Join(", ", result));Where method selects numbers 4 and 5 from the list.Select method transforms 4 to 8 and 5 to 10.var words = new List<string> {"apple", "banana", "cherry"};
var result = words.Where(w => w.Length > 5).Select(w => w.ToUpper);
Console.WriteLine(string.Join(", ", result));w.ToUpper without parentheses, but ToUpper is a method and needs ().Where and Select methods and lambda syntax are correct; string.Join works with IEnumerable.var students = new List<(string Name, int Score)>
{
("Alice", 85), ("Bob", 92), ("Charlie", 78), ("Diana", 92)
};
Which LINQ method syntax query returns a dictionary with scores as keys and list of student names who have that score as values?GroupBy method groups students sharing the same score.ToDictionary uses group key as dictionary key and selects student names as list for values.