SawNaw.LinqExtensions.EnumerableSplit
1.0.5
dotnet add package SawNaw.LinqExtensions.EnumerableSplit --version 1.0.5
NuGet\Install-Package SawNaw.LinqExtensions.EnumerableSplit -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SawNaw.LinqExtensions.EnumerableSplit" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SawNaw.LinqExtensions.EnumerableSplit" Version="1.0.5" />
<PackageReference Include="SawNaw.LinqExtensions.EnumerableSplit" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SawNaw.LinqExtensions.EnumerableSplit --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SawNaw.LinqExtensions.EnumerableSplit, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SawNaw.LinqExtensions.EnumerableSplit@1.0.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SawNaw.LinqExtensions.EnumerableSplit&version=1.0.5
#tool nuget:?package=SawNaw.LinqExtensions.EnumerableSplit&version=1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
LINQ extension to split a collection based on a given condition.
Think of it as a String.Split()
that works on collections of classes.
Example 1: Split a collection by specifying the splitters.
string[] things = { "pie", "mud-pie", "nuts", "plum", "mud-spread", "milk", "butter" };
// Literally specify the splitters.
var edibleThings = things.Split("mud-pie", "mud-spread");
edibleThings
will contain these collections:
{"pie"}
{"nuts", "plum"}
{"milk", "butter"}
Example 2: Split a collection by using a function to define the splitter.
public class Program
{
public static void Main(string[] args)
{
var records = new Person[]
{
new Person("John", "manager" ),
new Person("Mike Wang", "engineer"),
new Person("Separator 1", "NA" ),
new Person("Wilbur", "manager" ),
new Person("Shaun", "pilot" ),
new Person("Separator 2", "NA" ),
new Person("Mario", "manager" ),
};
var splitRecords = records.Split(r => r.Name.Contains("Separator"));
foreach (var collection in splitRecords)
{
foreach (var item in collection)
{
Console.WriteLine($"{item.Name} is a {item.Title}");
}
Console.WriteLine("***END OF COLLECTION***");
}
}
}
Output for Example 2
John is a manager
Mike Wang is a engineer
***END OF COLLECTION***
Wilbur is a manager
Shaun is a pilot
***END OF COLLECTION***
Mario is a manager
***END OF COLLECTION***
Use Cases
Any time a collection contains useful information separated by known elements.
For example, a text file may contain records where each record is separated by a newline.
Simply read the text file, and perform a Split()
, passing in the condition for the split.
string[] fileContents = File.ReadAllLines("records.txt");
var individualRecords = fileContents.Split(x => string.IsNullOrWhiteSpace(x));
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- No dependencies.
-
net7.0
- 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.
Add better examples