Class CommonUtils
Various utility functions
Inherited Members
Namespace: Cognite.Extractor.Common
Assembly: Cognite.Common.dll
Syntax
public static class CommonUtils
Methods
| Edit this page View SourceDistinctBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>?)
Returns elements of the source enumerable, where all elments have distinct results of selector
.
If there are duplicates, only the first occurence in the input enumerable will be used.
Declaration
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IEqualityComparer<TKey>? comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Input enumerable |
Func<TSource, TKey> | selector | Function to generate keys |
IEqualityComparer<TKey> | comparer | Optional element comparer |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Entries of the source enumerable, where all elements have distinct results of |
Type Parameters
Name | Description |
---|---|
TSource | |
TKey |
GetValue<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Try to get the value associated with the provided key
.
If it is not found, returns defaultValue
Declaration
public static TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dict | This dictionary |
TKey | key | Key to search |
TValue | defaultValue | Default value to return |
Returns
Type | Description |
---|---|
TValue | Value or default |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
GroupByTimeGranularity<T>(IEnumerable<T>, TimeSpan, Func<T, DateTime>, int)
Group input by some granularity, so that the result is a list of lists where
each inner list contains only elements within granularity
time of each other.
Declaration
public static IEnumerable<IEnumerable<T>> GroupByTimeGranularity<T>(this IEnumerable<T> items, TimeSpan granularity, Func<T, DateTime> tsSelector, int maxLength)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | items | Input enumerable |
TimeSpan | granularity | Granularity to chunk by |
Func<T, DateTime> | tsSelector | Function to get timestamp for each element |
int | maxLength | Maximum number of elements in each inner list |
Returns
Type | Description |
---|---|
IEnumerable<IEnumerable<T>> | A list of lists where each inner list contains only elements within |
Type Parameters
Name | Description |
---|---|
T | Input element type |
MinMax<T, R>(IEnumerable<T>, Func<T, R>)
Get the minimum and maximum value in a list, using selector
to get the value for
each item. Throws an exception if no elements are present.
Declaration
public static (R Min, R Max) MinMax<T, R>(this IEnumerable<T> items, Func<T, R> selector) where R : IComparable
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | items | Input enumerable |
Func<T, R> | selector | Function to get value for each element |
Returns
Type | Description |
---|---|
(R Min, R Max) | A tuple (Min, Max) |
Type Parameters
Name | Description |
---|---|
T | Input element type |
R | Comparable element |
TrimToNull(string?)
Trim this string object to null.
Declaration
public static string? TrimToNull(this string? @this)
Parameters
Type | Name | Description |
---|---|---|
string | this |
Returns
Type | Description |
---|---|
string | A string without leading or trailing whitespace, or null. |
WaitAsync(WaitHandle, TimeSpan, CancellationToken)
Convenient method to efficiently wait for a wait handle and cancellation token with timeout asynchronously.
Declaration
public static Task<bool> WaitAsync(WaitHandle handle, TimeSpan timeout, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
WaitHandle | handle | WaitHandle to wait for |
TimeSpan | timeout | Wait timeout |
CancellationToken | token | Cancellation token |
Returns
Type | Description |
---|---|
Task<bool> | True if wait handle or cancellation token was triggered, false otherwise |