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 |
SelectNonNull<TIn, TOut>(IEnumerable<TIn>, Func<TIn, TOut?>)
Select elements from TIn to TOut,
returning only when the result is not null. Unlike LINQ's Where + Select,
this is nullable-aware.
Declaration
public static IEnumerable<TOut> SelectNonNull<TIn, TOut>(this IEnumerable<TIn> source, Func<TIn, TOut?> map) where TOut : class
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TIn> | source | Source enumerable |
| Func<TIn, TOut> | map | Mapping function |
Returns
| Type | Description |
|---|---|
| IEnumerable<TOut> | Enumerable with non-null elements |
Type Parameters
| Name | Description |
|---|---|
| TIn | Source type |
| TOut | Target type |
SimplifyException(Exception)
Simplify an aggregate exception, flattening it and returning the inner exception if it only contains one.
Declaration
public static Exception SimplifyException(Exception ex)
Parameters
| Type | Name | Description |
|---|---|---|
| Exception | ex | Exception to simplify. |
Returns
| Type | Description |
|---|---|
| Exception | Simplified exception, or the original exception if it couldn't be simplified. |
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 |