ScyllaDB University Live | Free Virtual Training Event
Learn more
ScyllaDB Documentation Logo Documentation
  • Server
  • Cloud
  • Tools
    • ScyllaDB Manager
    • ScyllaDB Monitoring Stack
    • ScyllaDB Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
  • Resources
    • ScyllaDB University
    • Community Forum
    • Tutorials
Download
ScyllaDB Docs ScyllaDB C# driver Features Driver components Linq component Batch statements with LINQ

Batch statements with LINQ¶

This section shows how to use the LINQ API for batch statements. If you’re interested in reading more about how Batch Statements work in Cassandra, check the Batch Statements section.

Normal Batches¶

Let’s take the example shown in the LINQ overview documentation and add another class:

public class User
{
   public Guid UserId { get; set; }
   public string Name { get; set; }
}

public class UserByName : User
{
}

Let’s define the mappings:

public class MyMappings : Mappings
{
   public MyMappings()
   {
      For<User>()
         .KeyspaceName("ks_example")
         .TableName("users")
         .PartitionKey(u => u.UserId)
         .Column(u => u.UserId, cm => cm.WithName("id"))
         .Column(u => u.Name, cm => cm.WithName("name"));

      For<UserByName>()
         .KeyspaceName("ks_example")
         .TableName("users_by_name")
         .PartitionKey(u => u.UserId)
         .Column(u => u.UserId, cm => cm.WithName("id"))
         .Column(u => u.Name, cm => cm.WithName("name"));
   }
}

Don’t forget to actually define the MappingConfiguration and create the Table instances:

MappingConfiguration.Global.Define<MyMappings>();
var usersTable = new Table<User>(session);
var usersByNameTable = new Table<UserByName>(session);

Here is an example of creating a batch that inserts a user to both tables:

var user = new User
{
      Name = "john",
      UserId = Guid.NewGuid()
};

var userByName = new UserByName
{
      Name = user.Name,
      UserId = user.UserId
};

var batch = usersTable.GetSession().CreateBatch(BatchType.Logged);

batch.Append(usersTable.Insert(user));
batch.Append(usersByNameTable.Insert(userByName));

await batch.ExecuteAsync().ConfigureAwait(false);

Conditional Batches¶

The LINQ API doesn’t have support for conditional batches. It is still possible to execute conditional batches using the methods that are used in the previous example for normal batches but there is no way to obtain the response that the server returns for conditional batches.

You can use the Mapper without having to change your mapping configuration code (it works with both attribute based configuration and fluent based configuration just like LINQ) so you could use the Mapper for this use case. Check out the Conditional Batches section on the Mapper documentation.

Was this page helpful?

PREVIOUS
Linq component
NEXT
Mapper component
  • Create an issue
  • Edit this page

On this page

  • Batch statements with LINQ
    • Normal Batches
    • Conditional Batches
ScyllaDB C# driver
  • master
    • master
  • Features
    • Address resolution
    • Authentication and Authorization
    • Automatic failover
    • Column Encryption
    • Driver components
      • ADO.NET
      • Core component
        • Statements
          • Batch statements
          • Per-query keyspace
          • Prepared statements
          • Simple statements
      • Linq component
        • Batch statements with LINQ
      • Mapper component
        • Batch statements with the Mapper
    • Connection heartbeat
    • Connection pooling
    • CQL data types to C# types
      • Date and time representation
      • Nulls and unset
    • Execution Profiles
    • Graph support
    • Cluster and schema metadata
    • Metrics
      • App.Metrics Provider
      • List of metrics
    • Native protocol
    • OpenTelemetry
    • Result paging
    • Parameterized queries
    • Query timestamps
    • Query warnings
    • Request Tracker
    • Routing queries
    • Speculative query execution
    • TLS/SSL
    • Tuning policies
    • User-defined functions and aggregates
    • User-defined types
    • Vector support
  • FAQ
  • Upgrade Guide
  • Examples
  • API Reference
Docs Tutorials University Contact Us About Us
© 2025 ScyllaDB | Terms of Service | Privacy Policy | ScyllaDB, and ScyllaDB Cloud, are registered trademarks of ScyllaDB, Inc.
Last updated on 01 Aug 2025.
Powered by Sphinx 7.4.7 & ScyllaDB Theme 1.8.7
Ask AI