{"id":13730934,"url":"https://github.com/mariusschulz/ExtraLINQ","last_synced_at":"2025-05-08T03:32:18.914Z","repository":{"id":144141530,"uuid":"2130208","full_name":"mariusschulz/ExtraLINQ","owner":"mariusschulz","description":"Useful extension methods for .NET sequence and collection types.","archived":false,"fork":false,"pushed_at":"2018-11-10T11:04:47.000Z","size":3342,"stargazers_count":219,"open_issues_count":0,"forks_count":24,"subscribers_count":22,"default_branch":"master","last_synced_at":"2024-04-29T18:46:49.609Z","etag":null,"topics":["c-sharp","dotnet","linq"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mariusschulz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-07-30T21:19:28.000Z","updated_at":"2024-06-21T17:51:56.907Z","dependencies_parsed_at":null,"dependency_job_id":"bfbc30ea-5088-4d0d-a970-130c52232051","html_url":"https://github.com/mariusschulz/ExtraLINQ","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mariusschulz%2FExtraLINQ","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mariusschulz%2FExtraLINQ/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mariusschulz%2FExtraLINQ/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mariusschulz%2FExtraLINQ/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mariusschulz","download_url":"https://codeload.github.com/mariusschulz/ExtraLINQ/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224695828,"owners_count":17354490,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-sharp","dotnet","linq"],"created_at":"2024-08-03T02:01:21.635Z","updated_at":"2024-11-14T21:31:54.551Z","avatar_url":"https://github.com/mariusschulz.png","language":"C#","readme":"# ExtraLINQ\r\n\r\nExtraLINQ provides a set of extension methods for various .NET sequence types.\r\n\r\n\r\n## Installation\r\n\r\nExtraLINQ is [available as a NuGet package](http://www.nuget.org/packages/ExtraLINQ):\r\n\r\n```powershell\r\nInstall-Package ExtraLINQ\r\n```\r\n\r\n\r\n## Extensions\r\n\r\nExtensions for collections of type `IEnumerable\u003cT\u003e`:\r\n\r\n- [`Chunk`](#chunk)\r\n- [`Cycle`](#cycle)\r\n- [`Distinct`](#distinct)\r\n- [`Each`](#each)\r\n- [`Flatten`](#flatten)\r\n- [`HasExactly`](#hasexactly)\r\n- [`HasAtMost`](#hasatmost)\r\n- [`HasAtLeast`](#hasatleast)\r\n- [`Intersperse`](#intersperse)\r\n- [`IsEmpty`](#isempty)\r\n- [`IsNullOrEmpty`](#isnullorempty)\r\n- [`JoinedBy`](#joinedby)\r\n- [`None`](#none)\r\n- [`Partition`](#partition)\r\n- [`Random`](#random)\r\n- [`Repeat`](#repeat)\r\n- [`Shuffle`](#shuffle)\r\n- [`TakeSkip`](#takeskip)\r\n- [`ToHashSet`](#tohashset)\r\n- [`WhereNot`](#wherenot)\r\n- [`Without`](#without)\r\n\r\nExtensions for collections of type `NameValueCollection`:\r\n\r\n- [`ToDictionary`](#todictionary)\r\n- [`ToKeyValuePairs`](#tokeyvaluepairs)\r\n\r\n\r\n### Extensions for `IEnumerable\u003cT\u003e`\r\n\r\n\r\n#### `Chunk`\r\n\r\nSplits 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.\r\n\r\n```csharp\r\nint[] numbers = { 1, 2, 3, 4, 5, 6, 7 };\r\nint[][] chunks = numbers.Chunk(3).ToArray();\r\n\r\n// chunks = [[1, 2, 3], [4, 5, 6], [7]]\r\n```\r\n\r\n\r\n#### `Cycle`\r\n\r\nTurns a finite sequence into a circular one, or equivalently, repeats the original sequence indefinitely.\r\n\r\n```csharp\r\nint[] bits = { 0, 1 };\r\nint[] alternatingBits = bits.Cycle().Take(5).ToArray();\r\n\r\n// alternatingBits = [0, 1, 0, 1, 0]\r\n```\r\n\r\n\r\n#### `Distinct`\r\n\r\nReturns distinct elements from the given sequence using the default equality comparer to compare projected values.\r\n\r\n```csharp\r\nstring[] spellingsOfJavaScript = { \"JavaScript\", \"Javascript\", \"javascript\" };\r\nstring[] distinctSpellings = spellingsOfJavaScript\r\n    .Distinct(n =\u003e n.ToLower())\r\n    .ToArray();\r\n\r\n// distinctSpellings = [\"JavaScript\"]\r\n```\r\n\r\n\r\n#### `Each`\r\n\r\nPasses every element of the sequence to the specified action and returns it afterwards.\r\n\r\n```csharp\r\nstring[] ringInscriptionLines =\r\n{\r\n    \"One Ring to rule them all\",\r\n    \"One Ring to find them\",\r\n    \"One Ring to bring them all\",\r\n    \"and in the darkness bind them\"\r\n};\r\n\r\nringInscriptionLines.Each(Console.WriteLine);\r\n\r\n// Console output:\r\n//\r\n// One Ring to rule them all\r\n// One Ring to find them\r\n// One Ring to bring them all\r\n// and in the darkness bind them\r\n```\r\n\r\n\r\n#### `Flatten`\r\n\r\nReturns a flattened sequence that contains the concatenation of all the nested sequences' elements.\r\n\r\n```csharp\r\nint[][] numbers =\r\n{\r\n    new[] { 1, 2, 3 },\r\n    new[] { 4, 5 },\r\n    new[] { 6 }\r\n};\r\n\r\nint[] flattenedNumbers = numbers.Flatten().ToArray();\r\n\r\n// flattenedNumbers = [1, 2, 3, 4, 5, 6]\r\n```\r\n\r\n\r\n#### `HasAtLeast`\r\n\r\nDetermines whether a collection contains at least a certain number of items.\r\n\r\n```csharp\r\nstring[] theThreeRings = { \"Narya\", \"Nenya\", \"Vilya\" };\r\n\r\ntheThreeRings.HasAtLeast(0) // true\r\ntheThreeRings.HasAtLeast(2) // true\r\ntheThreeRings.HasAtLeast(4) // false\r\n```\r\n\r\nOptionally, a predicate can be passed that is called for every element:\r\n\r\n```csharp\r\nstring[] theThreeRings = { \"Narya\", \"Nenya\", \"Vilya\" };\r\n\r\ntheThreeRings.HasAtLeast(2, ring =\u003e ring.StartsWith(\"N\")) // true\r\ntheThreeRings.HasAtLeast(3, ring =\u003e ring.StartsWith(\"N\")) // false\r\n```\r\n\r\n\r\n#### `HasAtMost`\r\n\r\nDetermines whether a collection contains at most a certain number of items.\r\n\r\n```csharp\r\nstring[] theThreeRings = { \"Narya\", \"Nenya\", \"Vilya\" };\r\n\r\ntheThreeRings.HasAtMost(2) // false\r\ntheThreeRings.HasAtMost(3) // true\r\ntheThreeRings.HasAtMost(4) // true\r\n```\r\n\r\nOptionally, a predicate can be passed that is called for every element:\r\n\r\n```csharp\r\nstring[] theThreeRings = { \"Narya\", \"Nenya\", \"Vilya\" };\r\n\r\ntheThreeRings.HasAtMost(1, ring =\u003e ring.StartsWith(\"N\")) // false\r\ntheThreeRings.HasAtMost(2, ring =\u003e ring.StartsWith(\"N\")) // true\r\n```\r\n\r\n\r\n#### `HasExactly`\r\n\r\nDetermines whether a collection contains exactly a given number of items.\r\n\r\n```csharp\r\nstring[] theThreeRings = { \"Narya\", \"Nenya\", \"Vilya\" };\r\n\r\ntheThreeRings.HasExactly(2) // false\r\ntheThreeRings.HasExactly(3) // true\r\ntheThreeRings.HasExactly(4) // false\r\n```\r\n\r\nOptionally, a predicate can be passed that is called for every element:\r\n\r\n```csharp\r\nstring[] theThreeRings = { \"Narya\", \"Nenya\", \"Vilya\" };\r\n\r\ntheThreeRings.HasExactly(1, ring =\u003e ring.StartsWith(\"N\")) // false\r\ntheThreeRings.HasExactly(1, ring =\u003e ring.StartsWith(\"V\")) // true\r\ntheThreeRings.HasExactly(2, ring =\u003e ring.StartsWith(\"N\")) // true\r\n```\r\n\r\n\r\n#### `Intersperse`\r\n\r\nReturns all elements of the collection separated by the given separator.\r\n\r\n```csharp\r\nint[] numbers = { 1, 2, 3, 4, 5 };\r\nint[] separatedNumbers = numbers.Intersperse(0).ToArray();\r\n\r\n// separatedNumbers = [1, 0, 2, 0, 3, 0, 4, 0, 5]\r\n```\r\n\r\n\r\n#### `IsEmpty`\r\n\r\nDetermines whether a collection is empty.\r\n\r\n```csharp\r\nnew int[0].IsEmpty() // true\r\nnew[] { 1, 2, 3 }.IsEmpty() // false\r\n```\r\n\r\n\r\n#### `IsNullOrEmpty`\r\n\r\nDetermines whether a collection is null or empty.\r\n\r\n```csharp\r\n(null as int[]).IsNullOrEmpty() // true\r\nnew int[0].IsNullOrEmpty() // true\r\nnew[] { 1, 2, 3 }.IsNullOrEmpty() // false\r\n```\r\n\r\n#### `JoinedBy`\r\n\r\nConcatenates all items of a sequence using the specified separator between each item.\r\n\r\n```csharp\r\nstring[] nameParts = { \"The\", \"One\", \"Ring\" };\r\nstring ringName = nameParts\r\n    .Select(part =\u003e part.ToUpper())\r\n    .JoinedBy(\" \");\r\n\r\n// ringName = \"THE ONE RING\"\r\n```\r\n\r\nNote that the main purpose of `JoinedBy` is to provide a chainable wrapper around `String.Join`.\r\n\r\n\r\n#### `None`\r\n\r\nDetermines whether a collection doesn't contain any elements matching certain criteria.\r\n\r\n```csharp\r\nstring[] theThreeRings = { \"Narya\", \"Nenya\", \"Vilya\" };\r\nbool allRingsNamed = theThreeRings.None(string.IsNullOrWhiteSpace);\r\n\r\n// allRingsNamed = true\r\n```\r\n\r\nThe `None` method is equivalent to a negated version of `Any`.\r\n\r\n\r\n#### `Partition`\r\n\r\nUses the given predicate to partition the given sequence into two sequences, one with all the matches and one with all the mismatches.\r\n\r\n```csharp\r\nint[] numbers = { 1, 2, 3, 4, 5 };\r\nvar partitionedNumbers = numbers.Partition(n =\u003e n % 2 == 0);\r\n\r\n// partitionedNumbers.Matches = [2, 4]\r\n// partitionedNumbers.Mismatches = [1, 3, 5]\r\n```\r\n\r\n\r\n#### `Random`\r\n\r\nReturns a given number of random elements from a collection.\r\n\r\n```csharp\r\nint[] numbers = Enumerable.Range(1, 49).ToArray();\r\nint[] lottoNumbers = numbers\r\n    .Random(6)\r\n    .OrderBy(n =\u003e n)\r\n    .ToArray();\r\n\r\n// e.g. lottoNumbers = [5, 19, 20, 27, 38, 41]\r\n```\r\n\r\n\r\n#### `Repeat`\r\n\r\nRepeats a given sequence a given number of times.\r\n\r\n```csharp\r\nstring[] eatingSounds = { \"om\", \"nom\", \"nom\" };\r\nstring[] cookieMonsterSounds = eatingSounds.Repeat(3).ToArray();\r\n\r\n// cookieMonsterSounds = [\"om\", \"nom\", \"nom\", \"om\", \"nom\", \"nom\", \"om\", \"nom\", \"nom\"]\r\n```\r\n\r\n\r\n#### `Shuffle`\r\n\r\nEnumerates the specified input sequence and returns a new sequence which contains all input elements in random order.\r\n\r\n```csharp\r\nstring[] hobbits = { \"Frodo\", \"Sam\", \"Merry\", \"Pippin\" };\r\nstring[] shuffledHobbits = hobbits.Shuffle().ToArray();\r\n\r\n// e.g. shuffledHobbits = [\"Sam\", \"Pippin\", \"Frodo\", \"Merry\"]\r\n```\r\n\r\n\r\n#### `TakeSkip`\r\n\r\nIterates over the given sequence and repeatedly returns a specified number of elements before skipping a specified number of elements.\r\n\r\n```csharp\r\nstring[] fellowship =\r\n{\r\n    \"Frodo\",\r\n    \"Sam\",\r\n    \"Merry\",\r\n    \"Pippin\",\r\n    \"Aragorn\",\r\n    \"Legolas\",\r\n    \"Gimli\",\r\n    \"Boromir\",\r\n    \"Gandalf\"\r\n};\r\n\r\nstring[] everyOtherFellow = fellowship.TakeSkip(1, 1).ToArray();\r\n\r\n// everyOtherFellow = [\"Frodo\", \"Merry\", \"Aragorn\", \"Gimli\", \"Gandalf\"]\r\n```\r\n\r\n\r\n#### `ToHashSet`\r\n\r\nCreates a `HashSet` from a given sequence.\r\n\r\n```csharp\r\nstring gollumsUtterings = \"Nasty hobbitses, gollum, gollum!\";\r\nHashSet\u003cstring\u003e gollumsVocabulary = gollumsUtterings\r\n    .Split(new[] { ' ', ',', '!' }, StringSplitOptions.RemoveEmptyEntries)\r\n    .ToHashSet();\r\n\r\n// gollumsVocabulary = [\"Nasty\", \"hobbitses\", \"gollum\"]\r\n```\r\n\r\nNote that the main purpose of `ToHashSet` is to provide a chainable wrapper around the `HashSet` constructor.\r\n\r\n\r\n#### `WhereNot`\r\n\r\nFilters a sequence of values based on a given predicate and returns those values that don't match the predicate.\r\n\r\n```csharp\r\nstring[] theThreeRings = { \"Narya\", \"Nenya\", \"Vilya\" };\r\nFunc\u003cstring, bool\u003e startsWithN = value =\u003e value.StartsWith(\"N\");\r\nstring vilya = theThreeRings.WhereNot(startsWithN).Single();\r\n\r\n// vilya = \"Vilya\"\r\n```\r\n\r\n#### `Without`\r\n\r\nReturns the specified collection without the specified items.\r\n\r\n```csharp\r\nstring[] hobbits = { \"Frodo\", \"Sam\", \"Merry\", \"Pippin\" };\r\nstring[] mainHobbits = hobbits.Without(\"Merry\", \"Pippin\").ToArray();\r\n\r\n// mainHobbits = [\"Frodo\", \"Sam\"]\r\n```\r\n\r\n\r\n### Extensions for `NameValueCollection`\r\n\r\n\r\n#### `ToDictionary`\r\n\r\nReturns a new dictionary from the specified collection.\r\n\r\n```csharp\r\nvar ringBearers = new NameValueCollection\r\n{\r\n    { \"Nenya\", \"Galadriel\" },\r\n    { \"Narya\", \"Gandalf\" },\r\n    { \"Vilya\", \"Elrond\" }\r\n};\r\n\r\nDictionary\u003cstring, string\u003e ringBearersDictionary = ringBearers.ToDictionary();\r\n```\r\n\r\n\r\n#### `ToKeyValuePairs`\r\n\r\nEnumerates the specified collection as a sequence of key-value pairs.\r\n\r\n```csharp\r\nvar ringBearers = new NameValueCollection\r\n{\r\n    { \"Nenya\", \"Galadriel\" },\r\n    { \"Narya\", \"Gandalf\" },\r\n    { \"Vilya\", \"Elrond\" }\r\n};\r\n\r\nIEnumerable\u003cKeyValuePair\u003cstring, string\u003e\u003e ringBearersKeyValuePairs = ringBearers.ToKeyValuePairs();\r\n```\r\n","funding_links":[],"categories":["C#","Libraries"],"sub_categories":["Extensions"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmariusschulz%2FExtraLINQ","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmariusschulz%2FExtraLINQ","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmariusschulz%2FExtraLINQ/lists"}