LINQ Element Retrieval

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:20 AM on 8/8/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

24 Terms

1
New cards

First()

A LINQ element operator provided by Enumerable that returns the first element of a sequence and throws an InvalidOperationException if no matching element exists. Domain: C# → .NET → System.Linq → LINQ Element Retrieval

2
New cards

var result = numbers.First();

Returns the first element in the sequence and throws an InvalidOperationException if the sequence is empty.

3
New cards

var result = numbers.First(n => n > 10);

Returns the first element satisfying the specified condition and throws an InvalidOperationException if no matching element exists.

4
New cards

FirstOrDefault()

A LINQ element operator that returns the first element of a sequence, or the type's default value if no element exists.

5
New cards

var result = numbers.FirstOrDefault();

Returns the first number in the sequence, or the default value for the element type if the sequence is empty.

6
New cards

var result = numbers.FirstOrDefault(n => n > 10);

Returns the first element satisfying the condition, or the element type's default value if no matching element exists.

7
New cards

Last()

A LINQ element operator that returns the last element of a sequence and throws an InvalidOperationException if no matching element exists.

8
New cards

var result = numbers.Last();

Returns the last element of the sequence and throws an InvalidOperationException if the sequence is empty.

9
New cards

var result = numbers.Last(n => n < 100);

Returns the last element satisfying the specified condition and throws an InvalidOperationException if no matching element exists.

10
New cards

LastOrDefault()

A LINQ element operator that returns the last element of a sequence, or the type's default value if no element exists.

11
New cards

var result = numbers.LastOrDefault();

Returns the last element of the sequence, or the element type's default value if the sequence is empty.

12
New cards

var result = numbers.LastOrDefault(n => n < 100);

Returns the last element satisfying the condition, or the element type's default value if no matching element exists.

13
New cards

Single()

A LINQ element operator that requires exactly one matching element and throws an InvalidOperationException if zero or more than one matching element exists.

14
New cards

var result = numbers.Single();

Returns the sequence's only element and throws an InvalidOperationException if the sequence contains zero or more than one element.

15
New cards

var result = people.Single(p => p.Id == targetId);

Returns the only element satisfying the condition and throws an InvalidOperationException if zero or multiple elements satisfy it.

16
New cards

SingleOrDefault()

A LINQ element operator that returns the only matching element or the type's default value when no match exists, but still throws an InvalidOperationException when multiple matches exist.

17
New cards

var result = numbers.SingleOrDefault();

Returns the only element, returns the element type's default value if the sequence is empty, and throws an InvalidOperationException if multiple elements exist.

18
New cards

var result = people.SingleOrDefault(p => p.Id == targetId);

Returns the only matching element, returns the default value when no match exists, and throws an InvalidOperationException if multiple elements match.

19
New cards

ElementAt()

A LINQ element operator that returns the element at a specified zero-based position and throws an ArgumentOutOfRangeException when that position does not exist.

20
New cards

var result = numbers.ElementAt(3);

Returns the element at zero-based position 3, which is the fourth element in the sequence.

21
New cards

ElementAtOrDefault()

A LINQ element operator that returns the element at a specified zero-based position, or the element type's default value when that position does not exist.

22
New cards

var result = numbers.ElementAtOrDefault(10);

Returns the element at zero-based position 10, or the element type's default value if the sequence does not contain that position.

23
New cards

First() vs Single()

First() returns the first matching element even if additional matches exist, whereas Single() requires exactly one matching element and throws if multiple matches exist.

24
New cards

FirstOrDefault() vs SingleOrDefault()

FirstOrDefault() returns the first match and allows additional matches, whereas SingleOrDefault() requires at most one matching element and throws if multiple matches exist.