Get a collection (C#)
Gets a reference to a collection for use with the Data API clients.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Returns a Collection object that corresponds to the specified collection name.
This method returns a Collection object even for collections that don’t exist.
By default, the Collection object is typed as Collection<Document>, where Document is Dictionary<string, object>.
You can enable stronger typing by specifying a type when you get the collection.
For more information and examples, see Custom typing for collections.
Parameters
Use the GetCollection method, which belongs to the Database class.
Method signature
public Collection<Document> GetCollection(
string collectionName, GetCollectionOptions options = null
);
public Collection<T> GetCollection<T>(
string collectionName, GetCollectionOptions options = null
) where T : class;
public Collection<T> GetCollection<T>(
GetCollectionOptions options = null
) where T : class;
public Collection<T, TId> GetCollection<T, TId>(
GetCollectionOptions options = null
) where T : class;
public Collection<T, TId> GetCollection<T, TId>(
string collectionName, GetCollectionOptions options = null
) where T : class;
| Name | Type | Summary |
|---|---|---|
|
|
The name of the collection. If not specified, the client attempts to extract it from the |
|
Optional.
Options for this operation.
Keyspace is required unless you specified a working keyspace when instantiating the |
Examples
The following examples demonstrate how to get a collection.
Get a collection
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
}
public class Program
{
static void Main()
{
// Get a database
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
// Get a collection
var collection = database.GetCollection<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static void Main()
{
// Get a database
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
// Get a collection
var collection = database.GetCollection("COLLECTION_NAME");
}
}
Client reference
For more information, see the client reference.