All Projects → mariusschulz → Extralinq

mariusschulz / Extralinq

Licence: mit
Useful extension methods for .NET sequence and collection types.

Projects that are alternatives of or similar to Extralinq

Common Linq Mistakes
List of common LINQ mistakes
Stars: ✭ 109 (-46.04%)
Mutual labels:  linq
Fluentvalidation.blazor
Fluent Validation-powered Blazor component for validating standard <EditForm> 🌌 ✅
Stars: ✭ 140 (-30.69%)
Mutual labels:  linq
Nhibernate Core
NHibernate Object Relational Mapper
Stars: ✭ 1,918 (+849.5%)
Mutual labels:  linq
Servicestack.ormlite
Fast, Simple, Typed ORM for .NET
Stars: ✭ 1,532 (+658.42%)
Mutual labels:  linq
Data Forge Js
JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
Stars: ✭ 139 (-31.19%)
Mutual labels:  linq
Rangeless
c++ LINQ -like library of higher-order functions for data manipulation
Stars: ✭ 148 (-26.73%)
Mutual labels:  linq
Structlinq
Implementation in C# of LINQ concept with struct
Stars: ✭ 106 (-47.52%)
Mutual labels:  linq
Linq2db.entityframeworkcore
Bring power of Linq To DB to Entity Framework Core projects
Stars: ✭ 166 (-17.82%)
Mutual labels:  linq
Linq.extras
A set of extension methods to complement the ones from System.Linq.Enumerable
Stars: ✭ 139 (-31.19%)
Mutual labels:  linq
Framework
Open Source framework for writing data-centric applications over the latest technologies: .Net Core, C# 8 (not-nullable), ASP.NET Web API, Typescript 3.7 (strict), React, D3 and Sql Server or PostgreeSQL
Stars: ✭ 153 (-24.26%)
Mutual labels:  linq
Csharp Datatables Parser
C# Serverside parser for the popuplar jQuery datatables plugin.
Stars: ✭ 119 (-41.09%)
Mutual labels:  linq
Remote.linq
Simply LINQ your remote resources...
Stars: ✭ 136 (-32.67%)
Mutual labels:  linq
Jaque
Lets Java 8 Lambdas to be represented as objects in the form of expression trees at runtime
Stars: ✭ 152 (-24.75%)
Mutual labels:  linq
Impatient
Ain't nobody got time for data
Stars: ✭ 110 (-45.54%)
Mutual labels:  linq
Onemore
A OneNote add-in with simple, yet powerful and useful features
Stars: ✭ 163 (-19.31%)
Mutual labels:  linq
Linq
TypeScript Implementation of Language-Integrated Query (LINQ) (ECMAScript 2015)
Stars: ✭ 108 (-46.53%)
Mutual labels:  linq
Masuit.tools
ldqk.xyz/55
Stars: ✭ 2,539 (+1156.93%)
Mutual labels:  linq
Mvc.jquery.datatables
ASP MVC Helpers for connecting IQueryables with the amazing DataTables.net plugin
Stars: ✭ 179 (-11.39%)
Mutual labels:  linq
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+994.55%)
Mutual labels:  linq
Quill
Compile-time Language Integrated Queries for Scala
Stars: ✭ 1,998 (+889.11%)
Mutual labels:  linq

ExtraLINQ

ExtraLINQ provides a set of extension methods for various .NET sequence types.

Installation

ExtraLINQ is available as a NuGet package:

Install-Package ExtraLINQ

Extensions

Extensions for collections of type IEnumerable<T>:

Extensions for collections of type NameValueCollection:

Extensions for IEnumerable<T>

Chunk

Splits the given sequence into chunks of the given size. If the sequence length isn't evenly divisible by the chunk size, the last chunk will contain all remaining elements.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
int[][] chunks = numbers.Chunk(3).ToArray();

// chunks = [[1, 2, 3], [4, 5, 6], [7]]

Cycle

Turns a finite sequence into a circular one, or equivalently, repeats the original sequence indefinitely.

int[] bits = { 0, 1 };
int[] alternatingBits = bits.Cycle().Take(5).ToArray();

// alternatingBits = [0, 1, 0, 1, 0]

Distinct

Returns distinct elements from the given sequence using the default equality comparer to compare projected values.

string[] spellingsOfJavaScript = { "JavaScript", "Javascript", "javascript" };
string[] distinctSpellings = spellingsOfJavaScript
    .Distinct(n => n.ToLower())
    .ToArray();

// distinctSpellings = ["JavaScript"]

Each

Passes every element of the sequence to the specified action and returns it afterwards.

string[] ringInscriptionLines =
{
    "One Ring to rule them all",
    "One Ring to find them",
    "One Ring to bring them all",
    "and in the darkness bind them"
};

ringInscriptionLines.Each(Console.WriteLine);

// Console output:
//
// One Ring to rule them all
// One Ring to find them
// One Ring to bring them all
// and in the darkness bind them

Flatten

Returns a flattened sequence that contains the concatenation of all the nested sequences' elements.

int[][] numbers =
{
    new[] { 1, 2, 3 },
    new[] { 4, 5 },
    new[] { 6 }
};

int[] flattenedNumbers = numbers.Flatten().ToArray();

// flattenedNumbers = [1, 2, 3, 4, 5, 6]

HasAtLeast

Determines whether a collection contains at least a certain number of items.

string[] theThreeRings = { "Narya", "Nenya", "Vilya" };

theThreeRings.HasAtLeast(0) // true
theThreeRings.HasAtLeast(2) // true
theThreeRings.HasAtLeast(4) // false

Optionally, a predicate can be passed that is called for every element:

string[] theThreeRings = { "Narya", "Nenya", "Vilya" };

theThreeRings.HasAtLeast(2, ring => ring.StartsWith("N")) // true
theThreeRings.HasAtLeast(3, ring => ring.StartsWith("N")) // false

HasAtMost

Determines whether a collection contains at most a certain number of items.

string[] theThreeRings = { "Narya", "Nenya", "Vilya" };

theThreeRings.HasAtMost(2) // false
theThreeRings.HasAtMost(3) // true
theThreeRings.HasAtMost(4) // true

Optionally, a predicate can be passed that is called for every element:

string[] theThreeRings = { "Narya", "Nenya", "Vilya" };

theThreeRings.HasAtMost(1, ring => ring.StartsWith("N")) // false
theThreeRings.HasAtMost(2, ring => ring.StartsWith("N")) // true

HasExactly

Determines whether a collection contains exactly a given number of items.

string[] theThreeRings = { "Narya", "Nenya", "Vilya" };

theThreeRings.HasExactly(2) // false
theThreeRings.HasExactly(3) // true
theThreeRings.HasExactly(4) // false

Optionally, a predicate can be passed that is called for every element:

string[] theThreeRings = { "Narya", "Nenya", "Vilya" };

theThreeRings.HasExactly(1, ring => ring.StartsWith("N")) // false
theThreeRings.HasExactly(1, ring => ring.StartsWith("V")) // true
theThreeRings.HasExactly(2, ring => ring.StartsWith("N")) // true

Intersperse

Returns all elements of the collection separated by the given separator.

int[] numbers = { 1, 2, 3, 4, 5 };
int[] separatedNumbers = numbers.Intersperse(0).ToArray();

// separatedNumbers = [1, 0, 2, 0, 3, 0, 4, 0, 5]

IsEmpty

Determines whether a collection is empty.

new int[0].IsEmpty() // true
new[] { 1, 2, 3 }.IsEmpty() // false

IsNullOrEmpty

Determines whether a collection is null or empty.

(null as int[]).IsNullOrEmpty() // true
new int[0].IsNullOrEmpty() // true
new[] { 1, 2, 3 }.IsNullOrEmpty() // false

JoinedBy

Concatenates all items of a sequence using the specified separator between each item.

string[] nameParts = { "The", "One", "Ring" };
string ringName = nameParts
    .Select(part => part.ToUpper())
    .JoinedBy(" ");

// ringName = "THE ONE RING"

Note that the main purpose of JoinedBy is to provide a chainable wrapper around String.Join.

None

Determines whether a collection doesn't contain any elements matching certain criteria.

string[] theThreeRings = { "Narya", "Nenya", "Vilya" };
bool allRingsNamed = theThreeRings.None(string.IsNullOrWhiteSpace);

// allRingsNamed = true

The None method is equivalent to a negated version of Any.

Partition

Uses the given predicate to partition the given sequence into two sequences, one with all the matches and one with all the mismatches.

int[] numbers = { 1, 2, 3, 4, 5 };
var partitionedNumbers = numbers.Partition(n => n % 2 == 0);

// partitionedNumbers.Matches = [2, 4]
// partitionedNumbers.Mismatches = [1, 3, 5]

Random

Returns a given number of random elements from a collection.

int[] numbers = Enumerable.Range(1, 49).ToArray();
int[] lottoNumbers = numbers
    .Random(6)
    .OrderBy(n => n)
    .ToArray();

// e.g. lottoNumbers = [5, 19, 20, 27, 38, 41]

Repeat

Repeats a given sequence a given number of times.

string[] eatingSounds = { "om", "nom", "nom" };
string[] cookieMonsterSounds = eatingSounds.Repeat(3).ToArray();

// cookieMonsterSounds = ["om", "nom", "nom", "om", "nom", "nom", "om", "nom", "nom"]

Shuffle

Enumerates the specified input sequence and returns a new sequence which contains all input elements in random order.

string[] hobbits = { "Frodo", "Sam", "Merry", "Pippin" };
string[] shuffledHobbits = hobbits.Shuffle().ToArray();

// e.g. shuffledHobbits = ["Sam", "Pippin", "Frodo", "Merry"]

TakeSkip

Iterates over the given sequence and repeatedly returns a specified number of elements before skipping a specified number of elements.

string[] fellowship =
{
    "Frodo",
    "Sam",
    "Merry",
    "Pippin",
    "Aragorn",
    "Legolas",
    "Gimli",
    "Boromir",
    "Gandalf"
};

string[] everyOtherFellow = fellowship.TakeSkip(1, 1).ToArray();

// everyOtherFellow = ["Frodo", "Merry", "Aragorn", "Gimli", "Gandalf"]

ToHashSet

Creates a HashSet from a given sequence.

string gollumsUtterings = "Nasty hobbitses, gollum, gollum!";
HashSet<string> gollumsVocabulary = gollumsUtterings
    .Split(new[] { ' ', ',', '!' }, StringSplitOptions.RemoveEmptyEntries)
    .ToHashSet();

// gollumsVocabulary = ["Nasty", "hobbitses", "gollum"]

Note that the main purpose of ToHashSet is to provide a chainable wrapper around the HashSet constructor.

WhereNot

Filters a sequence of values based on a given predicate and returns those values that don't match the predicate.

string[] theThreeRings = { "Narya", "Nenya", "Vilya" };
Func<string, bool> startsWithN = value => value.StartsWith("N");
string vilya = theThreeRings.WhereNot(startsWithN).Single();

// vilya = "Vilya"

Without

Returns the specified collection without the specified items.

string[] hobbits = { "Frodo", "Sam", "Merry", "Pippin" };
string[] mainHobbits = hobbits.Without("Merry", "Pippin").ToArray();

// mainHobbits = ["Frodo", "Sam"]

Extensions for NameValueCollection

ToDictionary

Returns a new dictionary from the specified collection.

var ringBearers = new NameValueCollection
{
    { "Nenya", "Galadriel" },
    { "Narya", "Gandalf" },
    { "Vilya", "Elrond" }
};

Dictionary<string, string> ringBearersDictionary = ringBearers.ToDictionary();

ToKeyValuePairs

Enumerates the specified collection as a sequence of key-value pairs.

var ringBearers = new NameValueCollection
{
    { "Nenya", "Galadriel" },
    { "Narya", "Gandalf" },
    { "Vilya", "Elrond" }
};

IEnumerable<KeyValuePair<string, string>> ringBearersKeyValuePairs = ringBearers.ToKeyValuePairs();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].