Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Extension method: TResult FirstOrDefault<TSource, TResult>(this IQueryable<TSource>, Expression<Func<TSource, bool>>, Func<TSource, TResult>, TResult)

Declaration

public static TResult FirstOrDefault<TSource, TResult>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate,
    Func<TSource, TResult> resultSelector,
    TResult default
)

Summary

Returns the first element of a sequence, or a default value if the sequence contains no elements.

Generic type parameters

TSourceThe type of the elements of source.
TResultThe type of the resulting value.

Parameters

this IQueryable<TSource>sourceThe collection to return the first element of.
Expression<Func<TSource, bool>>predicateA function to test each element for a condition.
Func<TSource, TResult>resultSelectorA function to transform the first element into the result value. Will only be called if the sequence contains an element that passes the test specified by predicate.
TResultdefaultThe default value to return if the sequence contains no elements.

Returns

default if source is empty or if no element passes the test specified by predicate; otherwise, the transformed first element in source that passes the test specified by predicate.