{"id":13728770,"url":"https://github.com/kyubuns/ExtraLINQUnity","last_synced_at":"2025-05-08T01:30:21.476Z","repository":{"id":41256039,"uuid":"355044845","full_name":"kyubuns/ExtraLINQUnity","owner":"kyubuns","description":"Useful extension methods for .NET sequence and collection types.","archived":false,"fork":true,"pushed_at":"2024-10-03T08:14:03.000Z","size":3404,"stargazers_count":30,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-14T19:40:19.537Z","etag":null,"topics":["linq","unity"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"mariusschulz/ExtraLINQ","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kyubuns.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-06T03:28:22.000Z","updated_at":"2024-10-03T08:14:07.000Z","dependencies_parsed_at":"2023-01-23T10:45:02.171Z","dependency_job_id":null,"html_url":"https://github.com/kyubuns/ExtraLINQUnity","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/kyubuns%2FExtraLINQUnity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyubuns%2FExtraLINQUnity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyubuns%2FExtraLINQUnity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyubuns%2FExtraLINQUnity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kyubuns","download_url":"https://codeload.github.com/kyubuns/ExtraLINQUnity/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252981349,"owners_count":21835413,"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":["linq","unity"],"created_at":"2024-08-03T02:00:49.992Z","updated_at":"2025-05-08T01:30:20.736Z","avatar_url":"https://github.com/kyubuns.png","language":"C#","funding_links":[],"categories":["C#"],"sub_categories":[],"readme":"# ExtraLINQUnity\r\n\r\nThis is a repository for [mariusschulz/ExtraLINQ](https://github.com/mariusschulz/ExtraLINQ) that is easy to use with UnityPackageManager.\r\n\r\n```\r\n\"com.mariusschulz.extralinq\": \"https://github.com/kyubuns/ExtraLINQUnity.git?path=Unity/Assets/ExtraLINQ\"\r\n```\r\n\r\n-----\r\n\r\n# 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- [`FindIndex`](#findindex)\r\n- [`Flatten`](#flatten)\r\n- [`HasExactly`](#hasexactly)\r\n- [`HasAtMost`](#hasatmost)\r\n- [`HasAtLeast`](#hasatleast)\r\n- [`IndexOf`](#indexof)\r\n- [`Intersperse`](#intersperse)\r\n- [`IsEmpty`](#isempty)\r\n- [`IsNullOrEmpty`](#isnullorempty)\r\n- [`JoinedBy`](#joinedby)\r\n- [`MaxBy`](#maxby)\r\n- [`MaxByOrDefault`](#maxbyordefault)\r\n- [`MinBy`](#minby)\r\n- [`MinByOrDefault`](#minbyordefault)\r\n- [`None`](#none)\r\n- [`Partition`](#partition)\r\n- [`Random`](#random)\r\n- [`Repeat`](#repeat)\r\n- [`RotateLeft`](#rotateleft)\r\n- [`RotateRight`](#rotateright)\r\n- [`Shuffle`](#shuffle)\r\n- [`TakeSkip`](#takeskip)\r\n- [`ToHashSet`](#tohashset)\r\n- [`TryGetFirst`](#trygetfirst)\r\n- [`TryGetLast`](#trygetlast)\r\n- [`WhereNot`](#wherenot)\r\n- [`WhereNotNull`](#wherenotnull)\r\n- [`WithIndex`](#withindex)\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#### `RotateLeft`\r\n\r\nRotate Left\r\n\r\n```csharp\r\nint[] numbers = { 1, 2, 3, 4, 5 };\r\nint[] rotatedNumbers = numbers.RotateLeft(2).ToArray();\r\n\r\n// rotatedNumbers = [3, 4, 5, 1, 2]\r\n```\r\n\r\n\r\n#### `RotateRight`\r\n\r\nRotate Right\r\n\r\n```csharp\r\nint[] numbers = { 1, 2, 3, 4, 5 };\r\nint[] rotatedNumbers = numbers.RotateRight(1).ToArray();\r\n\r\n// rotatedNumbers = [5, 1, 2, 3, 4]\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#### `TryGetFirst`\r\n\r\nInstead of FirstOrDefault for structures like ValueTuple.\r\n\r\n```csharp\r\nif (items.TryGetFirst(x =\u003e x.Value == 5, out var target))\r\n{\r\n}\r\n```\r\n\r\n\r\n#### `TryGetLast`\r\n\r\nInstead of LastOrDefault for structures like ValueTuple.\r\n\r\n```csharp\r\nif (items.TryGetLast(x =\u003e x.Value == 5, out var target))\r\n{\r\n}\r\n```\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#### `WhereNotNull`\r\n\r\n#### `WithIndex`\r\n\r\nReturn with index.\r\n\r\n```csharp\r\nstring[] hobbits = { \"Frodo\", \"Sam\", \"Merry\", \"Pippin\" };\r\nstring[] hobbitsWithIndex = hobbits.WithIndex().ToArray();\r\n\r\n// hobbitsWithIndex = [(\"Frodo\", 0), (\"Sam\", 1), (\"Merry\", 2), (\"Pippin\", 3)]\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyubuns%2FExtraLINQUnity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkyubuns%2FExtraLINQUnity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyubuns%2FExtraLINQUnity/lists"}