Queryable Class
Namespace: System.Linq
Represents a query that can be executed against a data source. The Queryable class provides static methods that operate on an IQueryable
object.
Methods
-
Aggregate
(IQueryable<TSource> source, Expression<Func<TSource, TAccumulate, TAccumulate>> func)
Applies an accumulator function over a sequence.
-
Aggregate
(IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
-
Aggregate
(IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func, Expression<Func<TAccumulate, TResult>> selector)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified selector function is used to transform the final accumulator value.
-
All
(IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
Determines whether all elements of a sequence satisfy a condition.
-
Any
(IQueryable<TSource> source)
Determines whether any element of a sequence exists.
-
Any
(IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
Determines whether any element of a sequence satisfies a condition.
-
Average
(IQueryable<int> source)
Computes the average of a sequence of
int
values. -
Average
(IQueryable<Nullable<int>> source)
Computes the average of a sequence of nullable
int
values. -
Cast
(IQueryable source)
Casts the elements of a sequence to the specified type.
-
Concat
(IQueryable<TSource> source1, IQueryable<TSource> source2)
Concatenates two sequences.
-
Contains
(IQueryable<TSource> source, TSource item)
Determines whether a sequence contains a specified element.
-
Count
(IQueryable<TSource> source)
Returns the number of elements in a sequence.
-
Count
(IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
Returns the number of elements in a sequence that satisfy a condition.
-
DefaultIfEmpty
(IQueryable<TSource> source)
Returns the elements of the specified sequence or a default value in a singleton collection if the sequence is empty.
-
DefaultIfEmpty
(IQueryable<TSource> source, TSource defaultValue)
Returns the elements of the specified sequence or a specified default value in a singleton collection if the sequence is empty.
-
Distinct
(IQueryable<TSource> source)
Returns distinct elements from a sequence.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
Groups the elements of a sequence according to a specified key selector function.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
Groups the elements of a sequence according to a specified key selector function and key comparer.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector)
Groups the elements of a sequence according to a specified key selector function.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, IEqualityComparer<TKey> comparer)
Groups the elements of a sequence according to a specified key selector function and key comparer.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TGroup>> groupSelector, Expression<Func<TKey, IEnumerable<TGroup>, TResult>> resultSelector)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>> groupSelector)
Groups the elements of a sequence according to a specified key selector function.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>> groupSelector, IEqualityComparer<TKey> comparer)
Groups the elements of a sequence according to a specified key selector function and key comparer.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TGroup>> groupSelector, Expression<Func<TKey, IEnumerable<TGroup>> resultSelector, IEqualityComparer<TKey> comparer)
Groups the elements of a sequence according to a specified key selector function and key comparer, and creates a result value from each group.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>, TResult>> resultSelector)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>> groupSelector, Expression<Func<TKey, IEnumerable<TElement>, TResult>> resultSelector)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>> groupSelector, Expression<Func<TKey, IEnumerable<TElement>> resultSelector, IEqualityComparer<TKey> comparer)
Groups the elements of a sequence according to a specified key selector function and key comparer, and creates a result value from each group.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>, TResult>> resultSelector, IEqualityComparer<TKey> comparer)
Groups the elements of a sequence according to a specified key selector function and key comparer, and creates a result value from each group.
-
GroupBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>> groupSelector, Expression<Func<TKey, IEnumerable<TElement>> resultSelector, IEqualityComparer<TKey> comparer)
Groups the elements of a sequence according to a specified key selector function and key comparer, and creates a result value from each group.
-
GroupJoin
(IQueryable<TOuter> outer, IQueryable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector)
Correlates the elements of two sequences based on matching keys and groups the results.
-
GroupJoin
(IQueryable<TOuter> outer, IQueryable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector, IEqualityComparer<TKey> comparer)
Correlates the elements of two sequences based on matching keys and groups the results.
-
Join
(IQueryable<TOuter> outer, IQueryable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector)
Correlates the elements of two sequences based on matching keys.
-
Join
(IQueryable<TOuter> outer, IQueryable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector, IEqualityComparer<TKey> comparer)
Correlates the elements of two sequences based on matching keys.
-
Last
(IQueryable<TSource> source)
Returns the last element of a sequence.
-
LastOrDefault
(IQueryable<TSource> source)
Returns the last element of a sequence, or a default value if the sequence is empty.
-
LastOrDefault
(IQueryable<TSource> source, TSource defaultValue)
Returns the last element of a sequence, or a specified default value if the sequence is empty.
-
Max
(IQueryable<TSource> source)
Returns the maximum value of a sequence.
-
Max
(IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
Returns the maximum value of a sequence of
int
values. -
Min
(IQueryable<TSource> source)
Returns the minimum value of a sequence.
-
Min
(IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
Returns the minimum value of a sequence of
int
values. -
OfType
(IQueryable source)
Filters the elements of an
IEnumerable
based on a specified type. -
OrderBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
Sorts the elements of a sequence in ascending order.
-
OrderBy
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
Sorts the elements of a sequence in ascending order by using a specified comparer.
-
OrderByDescending
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
Sorts the elements of a sequence in descending order.
-
OrderByDescending
(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
Sorts the elements of a sequence in descending order by using a specified comparer.
-
Range
(int start, int count)
Generates a sequence of integral numbers within a specified range.
-
Repeat
(TElement element, int count)
Generates a sequence that contains one repeated value.
-
Reverse
(IQueryable<TSource> source)
Reverses the order of the elements in a sequence.
-
Select
(IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
Projects each element of a sequence into a new form.
-
SelectMany
(IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> selector)
Projects each element of a sequence to an
IEnumerable<TResult>
and flattens the resulting sequences into one sequence. -
SelectMany
(IQueryable<TSource> source, Expression<Func<TSource, TResult, TResult>> selector)
Projects each element of a sequence to a new form by incorporating the element's index.
-
SelectMany
(IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> selector, Expression<Func<TSource, IEnumerable<TResult>, TResult>> resultSelector)
Projects each element of a sequence to an
IEnumerable<TResult>
, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. -
SelectMany
(IQueryable<TSource> source, Expression<Func<TSource, TResult, TResult>> selector, Expression<Func<TSource, TResult, TResult>> resultSelector)
Projects each element of a sequence to a new form by incorporating the element's index.
-
Single
(IQueryable<TSource> source)
Returns the only element of a sequence, and a default value if the sequence is empty; throws an exception if there is more than one element in the sequence.
-
SingleOrDefault
(IQueryable<TSource> source)
Returns the only element of a sequence, or a default value if the sequence is empty; throws an exception if there is more than one element in the sequence.
-
SingleOrDefault
(IQueryable<TSource> source, TSource defaultValue)
Returns the only element of a sequence, or a specified default value if the sequence is empty; throws an exception if there is more than one element in the sequence.
-
Skip
(IQueryable<TSource> source, int count)
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
-
SkipWhile
(IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
Bypasses elements in a sequence as long as a specified condition is met and then returns the remaining elements.
-
SkipWhile
(IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
Bypasses elements in a sequence as long as a specified condition is met and then returns the remaining elements. The element's index is used in the predicate's evaluation.
-
Sum
(IQueryable<int> source)
Computes the sum of a sequence of
int
values. -
Sum
(IQueryable<Nullable<int>> source)
Computes the sum of a sequence of nullable
int
values. -
Take
(IQueryable<TSource> source, int count)
Returns a specified number of contiguous elements from the start of a sequence.
-
TakeWhile
(IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
Returns elements from a sequence as long as a specified condition is met.
-
TakeWhile
(IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
Returns elements from a sequence as long as a specified condition is met. The element's index is used in the predicate's evaluation.
-
ThenBy
(IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
Performs a subsequent ordering of the elements in a sequence in ascending order.
-
ThenBy
(IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
-
ThenByDescending
(IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
Performs a subsequent ordering of the elements in a sequence in descending order.
-
ThenByDescending
(IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
-
Union
(IQueryable<TSource> source1, IQueryable<TSource> source2)
Produces the set union of two sequences.
-
Union
(IQueryable<TSource> source1, IQueryable<TSource> source2, IEqualityComparer<TSource> comparer)
Produces the set union of two sequences by using the specified equality comparer.
-
Where
(IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
Filters a sequence of values based on a predicate.
-
Where
(IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
Filters a sequence of values based on a predicate. The element's index is used in the predicate's evaluation.
-
Zip
(IQueryable<TFirst> first, IQueryable<TSecond> second, Expression<Func<TFirst, TSecond, TResult>> resultSelector)
Zips together two sequences.
Example
The following example demonstrates how to use the Where
method to filter a list of numbers.
using System;
using System.Linq;
using System.Collections.Generic;
// Assume this is your data source, which implements IQueryable
IEnumerable<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Using Queryable.Where to filter even numbers
IQueryable<int> evenNumbersQuery = numbers.AsQueryable().Where(n => n % 2 == 0);
// To execute the query and get results (e.g., if this were a database query)
// In this simple example, it will just enumerate the IEnumerable
var result = evenNumbersQuery.ToList();
// result will contain: [2, 4, 6, 8, 10]