Margarida.Core.Linq
1.0.2
dotnet add package Margarida.Core.Linq --version 1.0.2
NuGet\Install-Package Margarida.Core.Linq -Version 1.0.2
<PackageReference Include="Margarida.Core.Linq" Version="1.0.2" />
<PackageVersion Include="Margarida.Core.Linq" Version="1.0.2" />
<PackageReference Include="Margarida.Core.Linq" />
paket add Margarida.Core.Linq --version 1.0.2
#r "nuget: Margarida.Core.Linq, 1.0.2"
#addin nuget:?package=Margarida.Core.Linq&version=1.0.2
#tool nuget:?package=Margarida.Core.Linq&version=1.0.2
Margarida Core Linq
Overview
Library based on Linq that allows you to transform, iterate and query using extensions and also provides some utility methods for .NET Core applications.
Installation
Margarida Core Linq is available as a NuGet package. You can install it using the NuGet Package Console window:
PM> Install-Package Margarida.Core.Linq
Extensions
For
foreach using range
Using kotlin language, when you need to iterate a range there is this native function:
for (int i in 0..9)
//do something 10x
In C# we need to use the conventional for
or use the enumerable extension Range
to produce the same effect.
for (var i = 0; i <= 9; i++)
//do something 10x
foreach (var i in Enumerable.Range(0,9))
//do something 10x
But using C# 9.0 we can create extensions for implicit indexes. In this package we include this improvement, and you can use that.
Usage
foreach (var i in 0..9)
//do something 10x
foreach using multiple variables
When we need iterate two or more sequences, there are a few ways to do it, like using conventional for
or combining Linq
extensions.
for (var i = 0; i < sequence1.Count(); i++)
{
var itemFromSequenceOne = sequence1[i];
var itemFromSequenceTwo = sequence2[i];
}
The problem with using this is that if you have more items in sequence than one another, an out-of-range exception can be a triggered. <br> <br> In this package has been created, an extension to create a tuple between the sequences, and now you can easily iterate the two.
Usage
foreach (var item in (sequence1,sequence2))
{
var itemFromSequence1 = item.item1;
var itemFromSequence1 = item.item2;
}
Select
Select with Action
Applies an action to an instance directly and project the instance.
Usage
var person = new Person { Name = "jonh" };
person.Select(x => x.Name = "jonh wick"); // set person name = "Jonh Wick"
Select with Function
Projects a element of a instance into a new form. </br> Invoking a transform function on to an instance and project the instance.
Usage
Person person = new Person { Name = "Jonh Wick" };
var name = person.Select(x => x.Name); // name = "Jonh Wick"
Tip 💡
Sometimes we need to execute a method and keep the instance, but we can't.
var instance = new Instance().SetPropertyValue(1); // SetPropertyValue dont have return.
// Error CS0815 Cannot assign void to an implicitly-typed variable
So we need to define the instance first, then call the method.
var instance = new Instance();
instance.SetPropertyValue(1);
Select with Action resolve this.
var instance = new Instance().Select(x => x.SetPropertyValue(1));
None
Determines whether none element of a sequence exists or satisfies a condition.
var list = new int[] { };
var listAreEmpty = list.None(); //true
list = new int[] { 0, 0, 0 };
var thereNoneGreaterThanZero = list.None(x => x > 0); //true
ToCompare
Compare each items in one sequence with the next.
Usage
var numbers = new[] { 1, 2, 3 };
var allPreviusIsLessThenNext = numbers.ToCompare((x, y) => x < y).All; // True
var anyPreviusIsLessThenNext = numbers.ToCompare((x, y) => x < y).Any; // True
var nonePreviusIsLessThenNext = numbers.ToCompare((x, y) => x < y).None;// False
///combining Linq.Extensions in ToCompare for bidirectional enumerables.
var first = new[] { 1, 2, 3 };
var second = new[] { 4, 5, 6 };
var sequenceList = new[] { first, second };
var noneItemInFirstAreInTheSecond = sequenceList.ToCompare((x, y) => x.Intersect(y).Any()).None;
ChunkBy
Splits the elements of a sequence into chunks of size at most size.
Usage
new[] { 1, 2, 3, 4 }.ChunkBy(2); // [ { 1, 2 }, { 3, 4 }]
DistinctBy
Returns distinct elements from a sequence by using a comparer.
Usage
new[] { 1, 1, 1, 2 }.DistinctBy(x => x); // { 1, 2 }
InsertAt
Insert into specific index a value, and move the next ones forward.
Usage
new[] { -1, 0, 1, 3 }.InsertAt(2, 3); // { -1, 0, 1, 2, 3 }
Shuffle
Shuffle a collection randomly.
Usage
new[] { -1, 0, 1, 3 }.Shuffle() // { 0, 1, 3, -1 }
ForEach
A simple way to write foreach statement in single-line.<br /> Executes a action or a body-action for each element in an enumerable.
Usage
new[] { 1, 2, 3 }.ForEach(x =>
Console.WriteLine(x)
);
Output:
1
2
3
Most
var list = new[] { 1, 3, 2, 4, 0, -1 };
int top;
top = list.Most((x, y) => x < y); // returns the smallest of a list.
Console.WriteLine(top);
top = list.Most((x, y) => x > y); // returns the biggest of a list.
Console.WriteLine(top);
Output:
-1
4
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.