CapyKit/Docs/api/CapyKit.Extensions.LINQExtensions.md

408 lines
13 KiB
Markdown

# <a id="CapyKit_Extensions_LINQExtensions"></a> Class LINQExtensions
Namespace: [CapyKit.Extensions](CapyKit.Extensions.md)
Assembly: CapyKit.dll
Provides static extension methods for performing common LINQ operations on <xref href="System.Collections.Generic.IEnumerable%601" data-throw-if-not-resolved="false"></xref> and <xref href="System.Linq.IQueryable%601" data-throw-if-not-resolved="false"></xref> collections.
```csharp
public static class LINQExtensions
```
#### Inheritance
[object](https://learn.microsoft.com/dotnet/api/system.object) ←
[LINQExtensions](CapyKit.Extensions.LINQExtensions.md)
#### Inherited Members
[object.Equals\(object?\)](https://learn.microsoft.com/dotnet/api/system.object.equals\#system\-object\-equals\(system\-object\)),
[object.Equals\(object?, object?\)](https://learn.microsoft.com/dotnet/api/system.object.equals\#system\-object\-equals\(system\-object\-system\-object\)),
[object.GetHashCode\(\)](https://learn.microsoft.com/dotnet/api/system.object.gethashcode),
[object.GetType\(\)](https://learn.microsoft.com/dotnet/api/system.object.gettype),
[object.MemberwiseClone\(\)](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone),
[object.ReferenceEquals\(object?, object?\)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals),
[object.ToString\(\)](https://learn.microsoft.com/dotnet/api/system.object.tostring)
#### Extension Methods
[ObjectExtensions.UpdateProperties\(object, object\)](CapyKit.Extensions.ObjectExtensions.md\#CapyKit\_Extensions\_ObjectExtensions\_UpdateProperties\_System\_Object\_System\_Object\_)
## Methods
### <a id="CapyKit_Extensions_LINQExtensions_Distinct__2_System_Collections_Generic_IEnumerable___0__System_Func___0___1__"></a> Distinct<T, U\>\(IEnumerable<T\>, Func<T, U\>\)
Enumerates distinct items in this collection as defined by the key <code class="paramref">property</code>.
```csharp
public static IEnumerable<T> Distinct<T, U>(this IEnumerable<T> items, Func<T, U> property)
```
#### Parameters
`items` [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
The items to act on.
`property` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<T, U\>
The property.
#### Returns
[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
An enumerator that allows foreach to be used to process distinct items in this collection.
#### Type Parameters
`T`
Generic type parameter of the parent object.
`U`
Generic type parameter property value.
### <a id="CapyKit_Extensions_LINQExtensions_Filter__1_System_Collections_Generic_IEnumerable___0__System_Func___0_System_Boolean__"></a> Filter<T\>\(IEnumerable<T\>, Func<T, bool\>\)
Filters out items matching a <code class="paramref">predicate</code> from the collection.
```csharp
public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> predicate)
```
#### Parameters
`source` [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
The source to act on.
`predicate` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<T, [bool](https://learn.microsoft.com/dotnet/api/system.boolean)\>
The predicate.
#### Returns
[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
An enumerator that allows foreach to be used to process remove in this collection.
#### Type Parameters
`T`
Generic type parameter.
### <a id="CapyKit_Extensions_LINQExtensions_Filter__1_System_Linq_IQueryable___0__System_Linq_Expressions_Expression_System_Func___0_System_Boolean___"></a> Filter<T\>\(IQueryable<T\>, Expression<Func<T, bool\>\>\)
Filters out items matching a <code class="paramref">predicate</code> from the collection.
```csharp
public static IQueryable<T> Filter<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate)
```
#### Parameters
`source` [IQueryable](https://learn.microsoft.com/dotnet/api/system.linq.iqueryable\-1)<T\>
The source to act on.
`predicate` [Expression](https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression\-1)<[Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<T, [bool](https://learn.microsoft.com/dotnet/api/system.boolean)\>\>
The predicate.
#### Returns
[IQueryable](https://learn.microsoft.com/dotnet/api/system.linq.iqueryable\-1)<T\>
An enumerator that allows foreach to be used to process remove in this collection.
#### Type Parameters
`T`
Generic type parameter.
### <a id="CapyKit_Extensions_LINQExtensions_LeftOuterJoin__4_System_Linq_IQueryable___0__System_Linq_IQueryable___1__System_Linq_Expressions_Expression_System_Func___0___2___System_Linq_Expressions_Expression_System_Func___1___2___System_Func___0_System_Collections_Generic_IEnumerable___1____3__System_Func___0___1__"></a> LeftOuterJoin<T, U, TKey, R\>\(IQueryable<T\>, IQueryable<U\>, Expression<Func<T, TKey\>\>, Expression<Func<U, TKey\>\>, Func<T, IEnumerable<U\>, R\>, Func<T, U\>\)
An IQueryable&lt;T&gt; extension method that left outer join.
```csharp
public static IQueryable<R> LeftOuterJoin<T, U, TKey, R>(this IQueryable<T> source, IQueryable<U> inner, Expression<Func<T, TKey>> outerSelector, Expression<Func<U, TKey>> innerSelector, Func<T, IEnumerable<U>, R> resultSelector, Func<T, U> defaultGenerator = null)
```
#### Parameters
`source` [IQueryable](https://learn.microsoft.com/dotnet/api/system.linq.iqueryable\-1)<T\>
The source to act on.
`inner` [IQueryable](https://learn.microsoft.com/dotnet/api/system.linq.iqueryable\-1)<U\>
The inner.
`outerSelector` [Expression](https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression\-1)<[Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<T, TKey\>\>
The outer selector.
`innerSelector` [Expression](https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression\-1)<[Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<U, TKey\>\>
The inner selector.
`resultSelector` [Func](https://learn.microsoft.com/dotnet/api/system.func\-3)<T, [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<U\>, R\>
The result selector.
`defaultGenerator` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<T, U\>
(Optional) The default generator.
#### Returns
[IQueryable](https://learn.microsoft.com/dotnet/api/system.linq.iqueryable\-1)<R\>
An IQueryable&lt;R&gt;
#### Type Parameters
`T`
Generic type parameter.
`U`
Generic type parameter.
`TKey`
Type of the key.
`R`
Type of the r.
### <a id="CapyKit_Extensions_LINQExtensions_LeftOuterJoin__4_System_Collections_Generic_IEnumerable___0__System_Collections_Generic_IEnumerable___1__System_Func___0___2__System_Func___1___2__System_Func___0_System_Collections_Generic_IEnumerable___1____3__System_Func___0___1__"></a> LeftOuterJoin<T, U, TKey, R\>\(IEnumerable<T\>, IEnumerable<U\>, Func<T, TKey\>, Func<U, TKey\>, Func<T, IEnumerable<U\>, R\>, Func<T, U\>\)
An IEnumable&lt;T&gt; extension method that left outer join.
```csharp
public static IEnumerable<R> LeftOuterJoin<T, U, TKey, R>(this IEnumerable<T> source, IEnumerable<U> inner, Func<T, TKey> outerSelector, Func<U, TKey> innerSelector, Func<T, IEnumerable<U>, R> resultSelector, Func<T, U> defaultGenerator = null)
```
#### Parameters
`source` [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
The source to act on.
`inner` [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<U\>
The inner.
`outerSelector` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<T, TKey\>
The outer selector.
`innerSelector` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<U, TKey\>
The inner selector.
`resultSelector` [Func](https://learn.microsoft.com/dotnet/api/system.func\-3)<T, [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<U\>, R\>
The result selector.
`defaultGenerator` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<T, U\>
(Optional) The default generator.
#### Returns
[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<R\>
An enumerator that allows foreach to be used to process left outter join in this collection.
#### Type Parameters
`T`
Generic type parameter.
`U`
Generic type parameter.
`TKey`
Type of the key.
`R`
Type of the r.
### <a id="CapyKit_Extensions_LINQExtensions_Page__1_System_Collections_Generic_IEnumerable___0__System_Int32_System_Int32_"></a> Page<T\>\(IEnumerable<T\>, int, int\)
Get a page of items from a collection, skipping <code class="paramref">pageNumber</code> pages of
<code class="paramref">pageSize</code> items per page.
```csharp
public static IEnumerable<T> Page<T>(this IEnumerable<T> source, int pageNumber, int pageSize)
```
#### Parameters
`source` [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
The source to act on.
`pageNumber` [int](https://learn.microsoft.com/dotnet/api/system.int32)
The page number to retrieve.
`pageSize` [int](https://learn.microsoft.com/dotnet/api/system.int32)
Number of items per page.
#### Returns
[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
An enumerator that allows foreach to be used to process page in this collection.
#### Type Parameters
`T`
Generic type parameter.
#### Remarks
This method uses natural numbering starting at page 1.
#### Exceptions
[ArgumentOutOfRangeException](https://learn.microsoft.com/dotnet/api/system.argumentoutofrangeexception)
Thrown when <code class="paramref">pageNumber</code> is less than <code>1</code> or if
<code class="paramref">pageSize</code> is less than
<code>1</code>.
### <a id="CapyKit_Extensions_LINQExtensions_Page__1_System_Linq_IQueryable___0__System_Int32_System_Int32_"></a> Page<T\>\(IQueryable<T\>, int, int\)
Get a page of items from a collection, skipping <code class="paramref">pageNumber</code> pages of
<code class="paramref">pageSize</code> items per page.
```csharp
public static IQueryable<T> Page<T>(this IQueryable<T> source, int pageNumber, int pageSize)
```
#### Parameters
`source` [IQueryable](https://learn.microsoft.com/dotnet/api/system.linq.iqueryable\-1)<T\>
The source to act on.
`pageNumber` [int](https://learn.microsoft.com/dotnet/api/system.int32)
The page number to retrieve.
`pageSize` [int](https://learn.microsoft.com/dotnet/api/system.int32)
.
#### Returns
[IQueryable](https://learn.microsoft.com/dotnet/api/system.linq.iqueryable\-1)<T\>
An enumerator that allows foreach to be used to process page in this collection.
#### Type Parameters
`T`
Generic type parameter.
#### Remarks
This method uses natural numbering starting at page 1.
#### Exceptions
[ArgumentOutOfRangeException](https://learn.microsoft.com/dotnet/api/system.argumentoutofrangeexception)
Thrown when <code class="paramref">pageNumber</code> is less than <code>1</code> or if
<code class="paramref">pageSize</code> is less than
<code>1</code>.
### <a id="CapyKit_Extensions_LINQExtensions_PageCount__1_System_Collections_Generic_IEnumerable___0__System_Int32_"></a> PageCount<T\>\(IEnumerable<T\>, int\)
The number of pages of <code class="paramref">pageSize</code> size in the given collection.
```csharp
public static int PageCount<T>(this IEnumerable<T> source, int pageSize)
```
#### Parameters
`source` [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
The source to act on.
`pageSize` [int](https://learn.microsoft.com/dotnet/api/system.int32)
Size of the page.
#### Returns
[int](https://learn.microsoft.com/dotnet/api/system.int32)
An int.
#### Type Parameters
`T`
Generic type parameter.
#### Exceptions
[ArgumentOutOfRangeException](https://learn.microsoft.com/dotnet/api/system.argumentoutofrangeexception)
Thrown when <code class="paramref">pageSize</code> is less than <code>1</code>.
### <a id="CapyKit_Extensions_LINQExtensions_PageCount__1_System_Linq_IQueryable___0__System_Int32_"></a> PageCount<T\>\(IQueryable<T\>, int\)
The number of pages of <code class="paramref">pageSize</code> size in the given collection.
```csharp
public static int PageCount<T>(this IQueryable<T> source, int pageSize)
```
#### Parameters
`source` [IQueryable](https://learn.microsoft.com/dotnet/api/system.linq.iqueryable\-1)<T\>
The source to act on.
`pageSize` [int](https://learn.microsoft.com/dotnet/api/system.int32)
Size of the page.
#### Returns
[int](https://learn.microsoft.com/dotnet/api/system.int32)
An int.
#### Type Parameters
`T`
Generic type parameter.
#### Exceptions
[ArgumentOutOfRangeException](https://learn.microsoft.com/dotnet/api/system.argumentoutofrangeexception)
Thrown when <code class="paramref">pageSize</code> is less than <code>1</code>.