DBaker.CosmosDbQueryConverter
1.0.0
dotnet add package DBaker.CosmosDbQueryConverter --version 1.0.0
NuGet\Install-Package DBaker.CosmosDbQueryConverter -Version 1.0.0
<PackageReference Include="DBaker.CosmosDbQueryConverter" Version="1.0.0" />
<PackageVersion Include="DBaker.CosmosDbQueryConverter" Version="1.0.0" />
<PackageReference Include="DBaker.CosmosDbQueryConverter" />
paket add DBaker.CosmosDbQueryConverter --version 1.0.0
#r "nuget: DBaker.CosmosDbQueryConverter, 1.0.0"
#:package DBaker.CosmosDbQueryConverter@1.0.0
#addin nuget:?package=DBaker.CosmosDbQueryConverter&version=1.0.0
#tool nuget:?package=DBaker.CosmosDbQueryConverter&version=1.0.0
DBaker.CosmosDbQueryConverter
Write native Cosmos DB SQL queries with automatic parameterization. No query builder abstractions - just Cosmos SQL with safe parameter handling.
Features
- Write real SQL - Use the full power of Cosmos DB SQL syntax, not a limited query builder API
- Automatic parameterization - Parameter injection protection without manual parameter management
- IntelliSense support - Lambda expressions give you autocomplete and prevent property name typos
- Smart array expansion - Arrays automatically expand into
INclause parameters - JSON property mapping - Automatically resolves
JsonPropertyandJsonPropertyNameattributes
Installation
dotnet add package DBaker.CosmosDbQueryConverter
Usage
Lambda Expressions (Recommended)
Write real Cosmos DB SQL with IntelliSense and automatic property mapping:
using DBaker.CosmosDbQueryConverter;
// Simple query with parameters and property names
var query = CosmosDbQuery.Convert((MyDocument c) =>
$"SELECT * FROM {c} WHERE {c.Status} = {myStatus}");
// Nested properties
var query = CosmosDbQuery.Convert((MyDocument c) =>
$"SELECT * FROM {c} WHERE {c.Address.City} = {myCity}");
// Array expansion for IN clauses
var query = CosmosDbQuery.Convert((MyDocument c) =>
$"SELECT * FROM {c} WHERE {c.Category} IN {myCategories}");
// Multi-line queries with JOINs
var query = CosmosDbQuery.Convert((MyDocument doc, SubDocument sub) =>
$"""
SELECT *
FROM {doc}
JOIN {sub} IN {doc.SubDoc}
WHERE {doc.Id} = {myId}
AND {sub.Status} = {myStatus}
""")
Properties are automatically mapped using JsonProperty (Newtonsoft.Json) or JsonPropertyName (System.Text.Json) attributes. If neither exist we fall back to the camel cased property name.
Interpolated Strings
Write native Cosmos SQL with interpolated values - all parameters are automatically handled:
var query = CosmosDbQuery.Convert(
$"SELECT * FROM c WHERE c.id = {myId} AND c.status = {myStatus}");
// Arrays work here too
var query = CosmosDbQuery.Convert(
$"SELECT * FROM c WHERE c.tag IN {myTags}")
Placeholder-based Queries
For dynamic query building with parameter arrays:
var query = CosmosDbQuery.Convert(
"SELECT * FROM c WHERE c.category IN {0} AND c.price >= {1}",
myCategories,
minPrice)
How It Works
You write native Cosmos DB SQL queries. The library handles all the parameterization work automatically - converting values to named parameters (@p0, @p1, etc.) and expanding arrays into multiple parameters for IN clauses.
Before:
$"SELECT * FROM {c} WHERE {c.Status} IN {myStatuses}"
After:
SELECT * FROM c WHERE c.status IN (@p0, @p1, @p2)
Executing Queries
The Convert methods return a standard QueryDefinition that you pass directly to the Cosmos DB SDK:
var query = CosmosDbQuery.Convert((MyDocument c) =>
$"SELECT * FROM {c} WHERE {c.Status} = {myStatus}");
var queryIterator = container.GetItemQueryIterator<MyDocument>(query);
Custom Field Name Resolution
Disable Camel Casing
By default, property names are converted to camel case. To use property names as-is:
var query = CosmosDbQuery.Convert(
(MyDocument c) => $"SELECT * FROM {c} WHERE {c.Status} = {myStatus}",
new DefaultFieldNameResolver(useCamelCase: false));
Custom Resolver
Create your own resolver by implementing IFieldNameResolver or inheriting from DefaultFieldNameResolver:
public class MyFieldNameResolver : DefaultFieldNameResolver
{
protected override string GetJsonPropertyName(MemberInfo member)
{
// Your custom logic here
return member.Name.ToLowerInvariant();
}
}
var query = CosmosDbQuery.Convert(
(MyDocument c) => $"SELECT * FROM {c} WHERE {c.Status} = {myStatus}",
new MyFieldNameResolver());
| Product | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Azure.Cosmos (>= 3.0.0)
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.0 | 179 | 11/23/2025 |