1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Where()
A LINQ filtering operator provided by Enumerable that returns only the elements of a sequence that satisfy a specified condition. Domain: C# → .NET → System.Linq → LINQ Filtering
var results = numbers.Where(n => n > 10);
Returns a sequence containing only numbers greater than 10.
var results = numbers.Where(n => n % 2 == 0);
Returns a sequence containing only even numbers.
var results = numbers.Where(n => n >= 10 && n <= 20);
Returns only elements whose values fall within the inclusive range from 10 through 20.
var results = numbers.Where(n => n < 0 || n > 100);
Returns elements satisfying either of the specified conditions.
var results = names.Where(name => name.StartsWith("A"));
Returns only strings that begin with the specified text.
var results = names.Where(name => name.Contains("son"));
Returns only strings containing the specified substring.
var results = names.Where(name => name.Length >= 5);
Returns only strings whose length is at least 5 characters.
var results = people.Where(person => person.Age >= 18);
Filters a sequence of objects according to the value of one of their properties.
var results = people.Where(p => p.Age >= 18 && p.IsActive);
Filters objects using multiple conditions that must all evaluate to true.
var results = items.Where(item => item != null);
Returns only elements that are not null.
var results = numbers.Where((number, index) => index % 2 == 0);
Uses the indexed overload of Where() to return elements located at even sequence indexes.
var results = numbers.Where((number, index) => number > index);
Uses both an element's value and its zero-based sequence index when deciding whether to include it.
var results = numbers.Where(n => n > 0).Where(n => n % 2 == 0);
Chains multiple Where() operations so an element must survive each successive filtering operation.
var results = values.OfType
Returns only elements that are compatible with string and exposes the resulting sequence as IEnumerable
var results = values.OfType
Returns only elements that are compatible with int from a sequence containing values of different runtime types.
OfType
A LINQ filtering operator that selects only elements that can be treated as the specified type T and excludes incompatible elements.
Where() vs OfType
Where() filters elements according to a Boolean condition, whereas OfType