Unnestable 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Unnestable --version 1.0.5
                    
NuGet\Install-Package Unnestable -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="Unnestable" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Unnestable" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Unnestable" />
                    
Project file
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 Unnestable --version 1.0.5
                    
#r "nuget: Unnestable, 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 Unnestable@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=Unnestable&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Unnestable&version=1.0.5
                    
Install as a Cake Tool

Dapper.Unnest.Generator

NuGet NuGet

A C# source generator that automatically generates extension methods to "unnest" collections of objects into arrays for each property. This is particularly useful for Dapper bulk operations where you need to pass arrays of values for each column.

Table of Contents

Installation

Install the NuGet package:

dotnet add package Unnestable

The package includes both the attribute library and the source generator.

How It Works

  1. Mark your class/record with the [Unnestable] attribute
  2. The source generator automatically creates:
    • A {YourType}Unnestable class with array properties for each public property
    • Extension methods ToUnnestable() on IEnumerable<YourType> and IReadOnlyCollection<YourType>

Usage

Basic Example

using Dapper.Unnest;

[Unnestable]
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

class Program
{
    static void Main()
    {
        var products = new[]
        {
            new Product { Id = 1, Name = "Laptop", Price = 999.99m },
            new Product { Id = 2, Name = "Mouse", Price = 29.99m },
        };

        var unnestable = products.ToUnnestable();

        // unnestable.Id contains [1, 2]
        // unnestable.Name contains ["Laptop", "Mouse"]
        // unnestable.Price contains [999.99m, 29.99m]
    }
}

Dapper Bulk Insert Example

[Unnestable]
public class User
{
    public string Email { get; set; }
    public string Name { get; set; }
    public DateTime CreatedAt { get; set; }
}

public async Task BulkInsertUsers(IEnumerable<User> users)
{
    var unnestable = users.ToUnnestable();

    using var connection = new SqlConnection(connectionString);
    await connection.ExecuteAsync(@"
        INSERT INTO Users (Email, Name, CreatedAt)
        SELECT Email, Name, CreatedAt
        FROM UNNEST(@Email, @Name, @CreatedAt) AS u(Email, Name, CreatedAt)",
        new
        {
            Email = unnestable.Email,
            Name = unnestable.Name,
            CreatedAt = unnestable.CreatedAt
        });
}

Supported Types

The generator supports:

  • Classes and Records: Both regular classes and record types
  • All Property Types: Primitive types, custom classes, structs, arrays, nullable types
  • Accessibility Modifiers: Matches the accessibility of the source type
  • Complex Types: Nested objects, collections, enums, etc.
  • Nullable Reference Types: Properly handles nullable and non-nullable reference types

Limitations

  • Only public properties with public getters are included (indexers are excluded)
  • Properties must have a getter method
  • The generated code inherits the accessibility of the source type

Examples

Arrays and Collections

[Unnestable]
public class Order
{
    public int OrderId { get; set; }
    public string[] Tags { get; set; }
    public List<string> Categories { get; set; }
}

var orders = new[]
{
    new Order { OrderId = 1, Tags = ["urgent", "priority"], Categories = ["electronics"] },
    new Order { OrderId = 2, Tags = ["normal"], Categories = ["books", "fiction"] }
};

var unnestable = orders.ToUnnestable();
// unnestable.OrderId: [1, 2]
// unnestable.Tags: [["urgent", "priority"], ["normal"]]
// unnestable.Categories: [["electronics"], ["books", "fiction"]]

Records and Structs

[Unnestable]
public record Person(string Name, int Age);

[Unnestable]
public struct Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

Nullable Types

[Unnestable]
public class OptionalData
{
    public string? Description { get; set; }
    public int? Count { get; set; }
}

Generated Code

For a class like:

[Unnestable]
public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
}

The generator produces:

public sealed class ItemUnnestable
{
    public int[] Id { get; set; } = Array.Empty<int>();
    public string[] Name { get; set; } = Array.Empty<string>();
}

public static class ItemUnnestExtensions
{
    public static ItemUnnestable ToUnnestable(this System.Collections.Generic.IEnumerable<Item> source, int count)
    {
        var idArray = new int[count];
        var nameArray = new string[count];

        int i = 0;
        foreach (var item in source)
        {
            idArray[i] = item.Id;
            nameArray[i] = item.Name;
            i++;
        }

        var result = new ItemUnnestable();
        result.Id = idArray;
        result.Name = nameArray;

        return result;
    }

    public static ItemUnnestable ToUnnestable(this System.Collections.Generic.IReadOnlyCollection<Item> source)
    {
        return source.ToUnnestable(source.Count);
    }
}

Performance

  • Memory Efficient: Uses exact-sized arrays based on collection count
  • Zero-Allocation for Arrays: Pre-allocates arrays and fills them in a single pass
  • Fast Execution: Simple loop-based transformation with minimal overhead
  • Type-Safe: Compile-time generated code with full type safety

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

  1. Clone the repository
  2. Run tests: dotnet test
  3. Build the solution: dotnet build

Running Tests

dotnet test Dapper.Unnest.Generator.Tests/Dapper.Unnest.Generator.Tests.csproj

License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❤️ for the .NET community

Product 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.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.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.

Version Downloads Last Updated
1.0.7 121 1/21/2026
1.0.5 134 1/20/2026 1.0.5 is deprecated because it is no longer maintained.
1.0.2 110 1/20/2026
1.0.1 114 1/20/2026