Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
The initial accumulator value.
An accumulator function to be invoked on each element.
A function to transform the final accumulator value into the result value.
The transformed final accumulator value.
["Jane", "Doe", "Joe"].Aggregate("", (res, item)=>res+" "+item, t=>t.toUpperCase().trim())
// "JANE DOE JOE"
Applies an accumulator function over a sequence.
An accumulator function to be invoked on each element.
The final accumulator value.
["Jane", "Doe", "Joe"].Aggregate((res, item)=>res+" "+item)
// " Jane Doe Joe"
Determines whether all elements of a sequence satisfy a condition.
A function to test each element for a condition.
true
if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false
.
[1,9,15].All(t=>t>5)
// false
[1,9,15].All(t=>t<50)
// true
Determines whether any element of a sequence exists or satisfies a condition.
A function to test each element for a condition.
true
if any elements in the source sequence pass the test in the specified predicate; otherwise, false
.
[1,9,15].Any(t=>t>5)
// true
[1,9,15].Any(t=>t>50)
// false
Appends a value to the end of the sequence.
The value to append to source.
A new sequence that ends with element.
[1,9,15].Append(20).ToArray()
// [1, 9, 15, 20]
Returns the input typed as IEnumerable<T>
.
The input sequence typed as IEnumerable<T>
.
Computes the average of a sequence of values.
A transform function to apply to each element.
The average of the sequence of values, or NaN
if the source sequence is empty or contains only values that are null.
[1,9,15].Average()
// 8.333333333333334
Casts the elements of an IEnumerable to the specified type. Useful for Typescript developers.
An IEnumerable<T>
that contains each element of the source sequence cast to the specified type.
Concatenates two sequences.
The sequence to concatenate to the first sequence.
An IEnumerable<T>
that contains the concatenated elements of the two sequences.
[1,9,15].Concat([20,21,25]).ToArray()
// [1, 9, 15, 20, 21, 25]
Determines whether a sequence contains a specified element.
The value to locate in the sequence.
An IEqualityComparer<T>
to compare values.
true
if the source sequence contains an element that has the specified value; otherwise, false.
[1,9,15].Contains(9)
// true
Returns the number of elements in a sequence.
A function to test each element for a condition.
The number of elements in the input sequence. A number that represents how many elements in the sequence satisfy the condition in the predicate function.
[1,9,15].Count()
// 3
[1,9,15].Count(t=>t>5)
// 2
Wrapper for custom projection or filtering.
A generator function which yields transformed source.
New IEnumerable<K>
yielded from generator function.
let people = [{Name:"Jack", Age:3}, {Name:"Joe", Age:2}, {Name:"Jane", Age:1}]
people.Custom(function* () {
// Output the names as many times as they are old
for (let t of this)
yield* Enumerable.Repeat(t.Name, t.Age)
}).Select(t=>t.toUpperCase()).ToArray()
// ["JACK", "JACK", "JACK", "JOE", "JOE", "JANE"]
Wrapper for custom aggregation of source sequence.
Function which aggregates sequence and returns a value.
Final value aggregated in function.
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
let oldest = people.Custom((source)=>{
// get the oldest person (aka MaxBy)
let currOldest = source.FirstOrDefault();
for (let t of source) if (t.Age > currOldest.Age) currOldest = t;
return currOldest;
})
oldest
// {Name:"Joe", Age:22}
Returns the elements of an IEnumerable
The value to return if the sequence is empty.
An IEnumerable<T>
that contains defaultValue if source is empty; otherwise, source.
[1,9,15].DefaultIfEmpty(5).ToArray()
// [1,9,15]
[].DefaultIfEmpty(5).ToArray()
// [5]
Returns distinct elements from a sequence.
An IEqualityComparer<T>
to compare values.
An IEnumerable<T>
that contains distinct elements from the source sequence.
[1,1,2,2,2,3,4,5,6,6].Distinct().ToArray()
// [1, 2, 3, 4, 5, 6]
Returns the element at a specified index in a sequence.
The zero-based index of the element to retrieve.
The element at the specified position in the source sequence.
[1,1,2,2,2,3,4,5,6,6].ElementAt(5)
// 3
Returns the element at a specified index in a sequence or a default value if the index is out of range.
The zero-based index of the element to retrieve.
Default value if the index is out of range.
Default value if the index is outside the bounds of the source sequence; otherwise, the element at the specified position in the source sequence.
[1,1,2,2,2,3,4,5,6,6].ElementAtOrDefault(20, 123)
// 123
[1,1,2,2,2,3,4,5,6,6].ElementAtOrDefault(8, 123)
// 6
Produces the set difference of two sequences.
An IEnumerable<T>
whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence.
An IEqualityComparer<T>
to compare values.
A sequence that contains the set difference of the elements of two sequences.
[1,1,2,2,2,3,4,5,6,6].Except([1,2,8]).ToArray()
// [3, 4, 5, 6]
Returns the first element of a sequence.
A function to test each element for a condition.
The first element in the sequence that passes the test in the specified predicate function.
[1,1,2,2,2,3,4,5,6,6].First()
// 1
[1,1,2,2,2,3,4,5,6,6].First(t=>t>2)
// 3
[].First()
// Error - Uncaught No first element
Returns the first element of a sequence, or a default value if no element is found.
A function to test each element for a condition.
Default value if no element is found.
Default value if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate.
[1,1,2,2,2,3,4,5,6,6].FirstOrDefault(t=>true, 123)
// 1
[1,1,2,2,2,3,4,5,6,6].FirstOrDefault(t=>t>2, 123)
// 3
[].FirstOrDefault(t=>true, 123)
// 123
Executes callback function on every element of a sequence.
Function to be executed on every element of a sequence.
[1,3,5].ForEach(t=>console.log(t))
// 1
// 3
// 5
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. Key values are compared by using a specified comparer, and the elements of each group are projected by using a specified function.
A function to extract the key for each element.
A function to map each source element to an element in an IGrouping<TKey,TElement>
.
A function to create a result value from each group.
An IEqualityComparer<TKey>
to compare keys with.
A collection of elements of type TResult
where each element represents a projection over a group and its key.
let ageGroupEqualityComparer = {
Equals: (x, y) => x.AgeGroup===y.AgeGroup,
GetHashCode: obj => obj.AgeGroup
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.GroupBy(t=>t.Age>20 ? {AgeGroup:'Young'} : {AgeGroup:'Old'}, t=>t.Name, (key, group)=>({Key:key, Names:[...group]}), ageGroupEqualityComparer).ToArray()
// [{"Key":{"AgeGroup":"Old"},"Names":["Jack","Jack"]},{"Key":{"AgeGroup":"Young"},"Names":["Joe"]}]
Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.
A function to extract the key for each element.
A function to map each source element to an element in the IGrouping<TKey,TElement>
.
An IEnumerable<IGrouping<TKey, TElement>>
where each IGrouping<TKey,TElement>
object contains a collection of objects of type TElement
and a key.
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.GroupBy(t=>t.Age>20 ? 'Young' : 'Old', t=>t.Name).Select(t=>({Key:t.Key, Names:t.ToArray()})).ToArray()
// [{"Key":"Old","Names":["Jack","Jack"]},{"Key":"Young","Names":["Joe"]}]
Groups the elements of a sequence according to a key selector function. The keys are compared by using a comparer and each group's elements are projected by using a specified function.
A function to extract the key for each element.
A function to map each source element to an element in an IGrouping<TKey,TElement>
.
An IEqualityComparer<T>
to compare keys.
An IEnumerable<IGrouping<TKey, TElement>>
where each IGrouping<TKey,TElement>
object contains a collection of objects of type TElement
and a key.
let ageGroupEqualityComparer = {
Equals: (x, y) => x.AgeGroup===y.AgeGroup,
GetHashCode: obj => obj.AgeGroup
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.GroupBy(t=>t.Age>20 ? {AgeGroup:'Young'} : {AgeGroup:'Old'}, t=>t.Name, ageGroupEqualityComparer).Select(t=>({Key:t.Key, Names:t.ToArray()})).ToArray()
[{"Key":{"AgeGroup":"Old"},"Names":["Jack","Jack"]},{"Key":{"AgeGroup":"Young"},"Names":["Joe"]}]
//
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The keys are compared by using a specified comparer.
A function to extract the key for each element.
A function to create a result value from each group.
An IEqualityComparer<T>
to compare keys with.
A collection of elements of type TResult
where each element represents a projection over a group and its key.
let ageGroupEqualityComparer = {
Equals: (x, y) => x.AgeGroup===y.AgeGroup,
GetHashCode: obj => obj.AgeGroup
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.GroupBy(t=>t.Age>20 ? {AgeGroup:'Young'} : {AgeGroup:'Old'}, (key, group)=>({Key:key, Names:[...group]}), ageGroupEqualityComparer).ToArray()
// [{"Key":{"AgeGroup":"Old"},"Names":[{"Name":"Jack","Age":18},{"Name":"Jack","Age":20}]},{"Key":{"AgeGroup":"Young"},"Names":[{"Name":"Joe","Age":22}]}]
Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.
A function to extract the key for each element.
An IEqualityComparer
An IEnumerable<IGrouping<TKey, TElement>>
where each IGrouping<TKey,TElement>
object contains a collection of objects of type TElement
and a key.
let ageGroupEqualityComparer = {
Equals: (x, y) => x.AgeGroup===y.AgeGroup,
GetHashCode: obj => obj.AgeGroup
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.GroupBy(t=>t.Age>20 ? {AgeGroup:'Young'} : {AgeGroup:'Old'}, ageGroupEqualityComparer).Select(t=>({Key:t.Key, Names:t.ToArray()})).ToArray()
// [{"Key":{"AgeGroup":"Old"},"Names":[{"Name":"Jack","Age":18},{"Name":"Jack","Age":20}]},{"Key":{"AgeGroup":"Young"},"Names":[{"Name":"Joe","Age":22}]}]
Correlates the elements of two sequences based on key equality and groups the results. A specified IEqualityComparer<T>
is used to compare keys.
The sequence to join to the first sequence.
A function to extract the join key from each element of the first sequence.
A function to extract the join key from each element of the second sequence.
A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence.
An IEqualityComparer
An IEnumerable<T>
that contains elements of type TResult
that are obtained by performing a grouped join on two sequences.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Hedlund, Magnus"},{Name:"Adams, Terry"},{Name:"Weiss, Charlotte"}]
let pets = [{Name:"Barley", Owner:{Name:"Adams, Terry"}},
{Name:"Boots", Owner:{Name:"Adams, Terry"}},
{Name:"Whiskers", Owner:{Name:"Weiss, Charlotte"}},
{Name:"Daisy", Owner:{Name:"Hedlund, Magnus"}},]
let res = people.GroupJoin(pets, person => person, pet=>pet.Owner,
(person, petCollection) => ({
OwnerName: person.Name,
Pets: [petCollection.Select(pet => pet.Name).Aggregate((seed,item)=>seed+" "+item).trim()]
}),
nameComparer)
.ToArray()
// [{"OwnerName":"Hedlund, Magnus","Pets":["Daisy"]},{"OwnerName":"Adams, Terry","Pets":["Barley Boots"]},{"OwnerName":"Weiss, Charlotte","Pets":["Whiskers"]}]
Produces the set intersection of two sequences.
An IEnumerable<T>
whose distinct elements that also appear in the first sequence will be returned.
An IEqualityComparer
A sequence that contains the elements that form the set intersection of two sequences.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.Intersect([{Name:"Joe", Age:50}, {Name:"Jane", Age:24}], nameComparer).ToArray()
// [{"Name":"Joe","Age":22}]
Correlates the elements of two sequences based on matching keys. A specified IEqualityComparer<T>
is used to compare keys.
The sequence to join to the first sequence.
A function to extract the join key from each element of the first sequence.
A function to extract the join key from each element of the second sequence.
A function to create a result element from two matching elements.
An IEqualityComparer<T>
to hash and compare keys.
An IEnumerable<T>
that has elements of type TResult
that are obtained by performing an inner join on two sequences.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Hedlund, Magnus"},{Name:"Adams, Terry"},{Name:"Weiss, Charlotte"},{Name:"Without Pets"}]
let pets = [{Name:"Barley", Owner:{Name:"Adams, Terry"}},
{Name:"Boots", Owner:{Name:"Adams, Terry"}},
{Name:"Pet Without Owner", Owner:{Name:"Doe, Jane"}},
{Name:"Whiskers", Owner:{Name:"Weiss, Charlotte"}},
{Name:"Daisy", Owner:{Name:"Hedlund, Magnus"}},];
people.Join(pets, person => person, pet=>pet.Owner,
(person, pet) => ({
OwnerName: person.Name,
Pet: pet.Name
}),
nameComparer)
.ToArray();
// [{"OwnerName":"Hedlund, Magnus","Pet":"Daisy"},{"OwnerName":"Adams, Terry","Pet":"Barley"},{"OwnerName":"Adams, Terry","Pet":"Boots"},{"OwnerName":"Weiss, Charlotte","Pet":"Whiskers"}]
Returns the last element of a sequence that satisfies a specified condition.
A function to test each element for a condition.
The last element in the sequence that passes the test in the specified predicate function.
[1,1,2,2,2,3,4,5,6,6].Last()
// 6
[1,1,2,2,2,3,4,5,6,6].Last(t=>t<3)
// 2
[].Last()
// Error - Uncaught No last element
Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
A function to test each element for a condition.
Default value if no element is found.
Default value if the sequence is empty or if no elements pass the test in the predicate function; otherwise, the last element that passes the test in the predicate function.
[1,1,2,2,2,3,4,5,6,6].LastOrDefault(t=>true, 123)
// 6
[1,1,2,2,2,3,4,5,6,6].LastOrDefault(t=>t<3, 123)
// 2
[].LastOrDefault(t=>t<3, 123)
// 123
Invokes a transform function on each element of a sequence and returns the maximum Single value.
A transform function to apply to each element.
The maximum value in the sequence.
[1,2,6,4,7,34,7,8].Max()
// 34
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.Max(t => t.Age)
// 22
Invokes a transform function on each element of a sequence and returns the minimum resulting value.
A transform function to apply to each element.
The minimum value in the sequence.
[1,2,6,4,7,34,7,8].Min()
// 1
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.Min(t => t.Age)
// 18
Filters the elements of an IEnumerable
based on a specified type.
The type to filter the elements of the sequence on.
An IEnumerable<T>
that contains elements from the input sequence of type TResult.
[4,1,"1",2,3,"3",4,5,2, true].OfType("string").ToArray()
// ["1", "3"]
[4,1,"1",2,3,"3",4,5,2, true].OfType("number").ToArray()
// [4, 1, 2, 3, 4, 5, 2]
[4,1,"1",2,3,"3",4,5,2, true].OfType("boolean").ToArray()
[4,1,"1",true, new Set([1,2])].OfType("Set").ToArray()
// Set {1, 2}
Sorts the elements of a sequence in ascending order.
A function to extract a key from an element.
An IComparer<TKey>
to compare keys.
An IOrderedEnumerable<T>
whose elements are sorted in ascending according to a key.
["Jane", "Doe", "Joe"].OrderBy(t=>t).ToArray()
// ["Doe", "Jane", "Joe"]
Sorts the elements of a sequence in descending order.
A function to extract a key from an element.
An IComparer<TKey>
to compare keys.
An IOrderedEnumerable<T>
whose elements are sorted in descending order according to a key.
["Jane", "Doe", "Joe"].OrderByDescending(t=>t).ToArray()
// ["Joe", "Jane", "Doe"]
Adds a value to the beginning of the sequence.
The value to prepend to source.
A new sequence that begins with element.
[1,9,15].Prepend(20).ToArray()
// [20, 1, 9, 15]
Inverts the order of the elements in a sequence.
A sequence whose elements correspond to those of the input sequence in reverse order.
[1,9,15].Reverse().ToArray()
// [15, 9, 1]
Projects each element of a sequence into a new form by incorporating the element's index.
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
An IEnumerable<T>
whose elements are the result of invoking the transform function on each element of source.
[1,2,3].Select(t=>t*2).ToArray()
// [2, 4, 6]
[1,2,3].Select((t,i)=>i+" => "+t).ToArray()
// ["0 => 1", "1 => 2", "2 => 3"]
Projects each element of a sequence to an IEnumerable<T>
, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. The index of each source element is used in the intermediate projected form of that element.
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
A transform function to apply to each element of the intermediate sequence.
An IEnumerable<TResult>
whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of source and then mapping each of those sequence elements and their corresponding source element to a result element.
let people = [
{ Name:"Jane", Items: [ "a", "b" ] },
{ Name:"Jack", Items: [ "c", "d" ] },
{ Name:"Joe", Items: [ "e", "f" ] },
{ Name:"John", Items: [ "g" ] },
]
people.SelectMany((t,i) => t.Items.Select((z)=>z+i), (per, item) => ({ name:per.Name, item })).ToArray();
// [{"name":"Jane","item":"a0"},{"name":"Jane","item":"b0"},{"name":"Jack","item":"c1"},
// {"name":"Jack","item":"d1"},{"name":"Joe","item":"e2"},{"name":"Joe","item":"f2"},
// {"name":"John","item":"g3"}]
Projects each element of a sequence to an IEnumerable<T>
, and flattens the resulting sequences into one sequence. The index of each source element is used in the projected form of that element.
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
An IEnumerable<T>
whose elements are the result of invoking the one-to-many transform function on each element of an input sequence.
let people = [
{ Name:"Jane", Items: [ "a", "b" ] },
{ Name:"Jack", Items: [ "c", "d" ] },
{ Name:"Joe", Items: [ "e", "f" ] },
{ Name:"John", Items: [ "g" ] },
]
people.SelectMany((t,i) => t.Items.Select((z)=>z+i)).ToArray();
// ["a0", "b0", "c1", "d1", "e2", "f2", "g3"]
Determines whether two sequences are equal by comparing their elements by using a specified IEqualityComparer<T>
.
An IEnumerable<T>
to compare to the first sequence.
An IEqualityComparer<T>
to use to compare elements.
true if the two source sequences are of equal length and their corresponding elements compare equal according to comparer; otherwise, false.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.SequenceEqual([{Name:"Jack"}, {Name:"Joe"}, {Name:"Jack"}], nameComparer)
// true
people.SequenceEqual([{Name:"Jack"}, {Name:"Jack"}], nameComparer)
// false
Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
A function to test an element for a condition.
The single element of the input sequence that satisfies a condition.
[1,2,3,4].Single(t=>t>3)
// 4
[1,2,3,4].Single(t=>t>2)
// Error - Uncaught More than 1 element
[1].Single()
// 1
[1,2].Single()
// Error - Uncaught More than 1 element
[].Single()
// Error - Uncaught No element
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.
A function to test an element for a condition.
Default value if no element is found.
The single element of the input sequence that satisfies the condition, or default value if no such element is found.
[1,2,3,4].SingleOrDefault(t=>t>3, 123)
// 4
[1,2,3,4].SingleOrDefault(t=>t>2, 123)
// Error - Uncaught More than 1 element
[1].SingleOrDefault()
// 1
[1].SingleOrDefault(t=>true, 123)
// 1
[1,2].SingleOrDefault()
// Error - Uncaught More than 1 element
[].SingleOrDefault(t=>true, 123)
// 123
[].SingleOrDefault()
// null
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
The number of elements to skip before returning the remaining elements.
An IEnumerable<T>
that contains the elements that occur after the specified index in the input sequence.
[1,2,3,4,5].Skip(4).ToArray()
// [5]
Skip a specified number of elements at the end of a sequence.
The number of elements to skip at the end of a sequence.
An IEnumerable<T>
that contains the elements that occur at the beginning of a sequence without a specified number of elements at the end.
[1,2,3,4,5].SkipLast(2).ToArray()
// [1, 2, 3]
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. The element's index is used in the logic of the predicate function.
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
An IEnumerable<T>
that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate.
[1,2,3,4,5].SkipWhile((t,i) => i < 3).ToArray()
// [4, 5]
[1,2,3,4,5].SkipWhile((t) => t < 3).ToArray()
// [3, 4, 5]
Computes the sum of the sequence of numeric values that are obtained by invoking a transform function on each element of the input sequence.
A transform function to apply to each element.
The sum of the projected values.
[1,2,3,4,5].Sum()
// 15
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.Sum(t=>t.Age)
// 60
Returns a specified number of contiguous elements from the start of a sequence.
The number of elements to return.
An IEnumerable<T>
that contains the specified number of elements from the start of the input sequence.
[1,2,3,4,5].Take(3).ToArray()
// [1, 2, 3]
Take a specified number of elements at the end of a sequence.
The number of elements to return.
An IEnumerable<T>
that contains the specified number of elements from the end of the input sequence.
[1,2,3,4,5].TakeLast(3).ToArray()
// [3, 4, 5]
Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic of the predicate function.
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
An IEnumerable<T>
that contains elements from the input sequence that occur before the element at which the test no longer passes.
[1,5,9,12,45,6].TakeWhile((t,i)=>i<5).ToArray()
// [1, 5, 9, 12, 45]
[1,5,9,12,45,6].TakeWhile((t)=>t<12).ToArray()
// [1, 5, 9]
Creates an array from a IEnumerable<T>
.
An array that contains the elements from the input sequence.
new Set([1,5,9,8]).ToArray()
// [1, 5, 9, 8]
Creates a Dictionary<TKey,TValue>
from an IEnumerable<T>
according to a specified key selector function, a comparer, and an element selector function.
A function to extract a key from each element.
A transform function to produce a result element value from each element.
An IEqualityComparer<T>
to compare keys.
A Dictionary<TKey,TValue>
that contains values of type TValue
selected from the input sequence.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
people.ToDictionary(t=>({Name:t.Name}), t=>t.Age, nameComparer).ToArray()
// [{"Key":{"Name":"Jack"},"Value":18},{"Key":{"Name":"Joe"},"Value":22},{"Key":{"Name":"Jane"},"Value":20}]
people.ToDictionary(t=>t.Name, t=>t.Age).ToArray()
// [{"Key":"Jack","Value":18},{"Key":"Joe","Value":22},{"Key":"Jane","Value":20}]
Creates a Dictionary<TKey,TValue>
from an IEnumerable<T>
according to a specified key selector function and key comparer.
A function to extract a key from each element.
An IEqualityComparer<T>
to compare keys.
A Dictionary<TKey,TValue>
that contains keys and values.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
people.ToDictionary(t=>({Name:t.Name}), nameComparer).ToArray()
// [{"Key":{"Name":"Jack"},"Value":{"Name":"Jack","Age":18}},{"Key":{"Name":"Joe"},"Value":{"Name":"Joe","Age":22}},{"Key":{"Name":"Jane"},"Value":{"Name":"Jane","Age":20}}]
people.ToDictionary(t=>t.Name).ToArray()
// [{"Key":"Jack","Value":{"Name":"Jack","Age":18}},{"Key":"Joe","Value":{"Name":"Joe","Age":22}},{"Key":"Jane","Value":{"Name":"Jane","Age":20}}]
Creates a HashSet<T>
from an IEnumerable<T>
using the comparer to compare keys.
An IEqualityComparer
A HashSet<T>
that contains values of type T
selected from the input sequence.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.ToHashSet(nameComparer).ToArray()
// [{"Name":"Jack","Age":18},{"Name":"Joe","Age":22}]
Creates a List<T>
from an IEnumerable<T>
.
An IComparer<TKey>
to compare keys.
A List<T>
that contains elements from the input sequence.
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
let mylist = people.ToList()
mylist.Add({Name:"Jane", Age:19})
mylist.ToArray()
// [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}, {Name:"Jane", Age:19}]
Creates a Lookup<TKey,TElement>
from an IEnumerable<T>
according to a specified key selector function, a comparer and an element selector function.
A function to extract a key from each element.
A transform function to produce a result element value from each element.
An IEqualityComparer<T>
to compare keys.
A Lookup<TKey,TElement>
that contains values of type TElement
selected from the input sequence.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.ToLookup(t=>({Name:t.Name}), t=>t.Age, nameComparer).Select(t=>({Name:t.Key.Name, Ages:t.ToArray()})).ToArray()
// [{"Name":"Jack","Ages":[18,20]},{"Name":"Joe","Ages":[22]}]
people.ToLookup(t=>t.Name, t=>t.Age).Select(t=>({Name:t.Key, Ages:t.ToArray()})).ToArray()
// [{"Name":"Jack","Ages":[18,20]},{"Name":"Joe","Ages":[22]}]
Creates a Lookup<TKey,TElement>
from an IEnumerable<T>
according to a specified key selector function and key comparer.
A function to extract a key from each element.
An IEqualityComparer<T>
to compare keys.
A Lookup<TKey,TElement>
that contains keys and values.
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.ToLookup(t=>({Name:t.Name}), nameComparer).Select(t=>({Name:t.Key.Name, Ages:t.ToArray()})).ToArray()
// [{"Name":"Jack","Ages":[{"Name":"Jack","Age":18},{"Name":"Jack","Age":20}]},{"Name":"Joe","Ages":[{"Name":"Joe","Age":22}]}]
people.ToLookup(t=>t.Name).Select(t=>({Name:t.Key, Ages:t.ToArray()})).ToArray()
// [{"Name":"Jack","Ages":[{"Name":"Jack","Age":18},{"Name":"Jack","Age":20}]},{"Name":"Joe","Ages":[{"Name":"Joe","Age":22}]}]
Creates a Map<TKey,TElement>
from an IEnumerable<T>
according to a specified key selector function and an element selector function.
A function to extract a key from each element.
A transform function to produce a result element value from each element.
A Map<TKey,TElement>
that contains values of type TElement
selected from the input sequence.
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
people.ToMap(t=>t.Name, t=>t.Age)
// Map {"Jack" => 18, "Joe" => 22, "Jane" => 20}
people.ToMap(t=>t.Name)
// Map {"Jack" => {"Name":"Jack","Age":18}, "Joe" => {"Name":"Joe","Age":22}, "Jane" => {"Name":"Jane","Age":20}}
Creates a Set<T>
from an IEnumerable<T>
.
A HashSet<T>
that contains values of type T
selected from the input sequence.
[1,2,5,3,2,5,1].ToSet()
// Set {1, 2, 5, 3}
Produces the set union of two sequences by using a specified IEqualityComparer<T>
.
An IEnumerable<T>
whose distinct elements form the second set for the union.
The IEqualityComparer<T>
to compare values.
An IEnumerable<T>
that contains the elements from both input sequences, excluding duplicates.
[1,2,5,2].Union([3,4,5]).ToArray()
// [1, 2, 5, 3, 4]
let nameComparer = {
Equals: (a,b) => a.Name===b.Name,
GetHashCode: a => a.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
people.Union([{Name:"Jack", Age:35}, {Name:"Jane", Age:23}], nameComparer).ToArray()
// [{"Name":"Jack","Age":18},{"Name":"Joe","Age":22},{"Name":"Jane","Age":23}]
Filters a sequence of values based on a predicate.
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
An IEnumerable<T>
that contains elements from the input sequence that satisfy the condition.
[1,5,6,4,3,2,1,8,9].Where((t,i)=>t>3 && i>2).ToArray()
// [4, 8, 9]
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
The second sequence to merge.
A function that specifies how to merge the elements from the two sequences.
An IEnumerable<T>
that contains merged elements of two input sequences.
let names = ["Jack", "Jane", "Joe"]
let ages = [20, 22, 25]
names.Zip(ages, (Name, Age)=>({Name, Age})).ToArray()
// [{"Name":"Jack","Age":20},{"Name":"Jane","Age":22},{"Name":"Joe","Age":25}]
Returns an enumerator that iterates through a collection.
Generated using TypeDoc
Exposes an enumerator, which supports a simple iteration over a non-generic collection. Is a base class for collection types and implements LINQ methods.