We did have to move away from Linq-Queries to our DocumentDB/CosmosDB.
Reason being mainly two use cases:
- Partial select - the document has at least one large field that I only want returned in certain cases. Specifying the fields directly saves RU costs. I was unable to achieve that in Linq.
Joins like this (example is a bit weird).
SqlQuerySpec spec = new SqlQuerySpec(@" SELECT value(n) FROM books b join p in b.author.parents where b.isbn = @isbnId AND lower(p.address.street) = @parentStreet ");
So our queries look something like this:
IQueryable<Book> queryable = client.CreateDocumentQuery<Book>(
collectionSelfLink,
new SqlQuerySpec
{
QueryText = "SELECT * FROM books b WHERE (b.Author.Name = @name)",
Parameters = new SqlParameterCollection()
{
new SqlParameter("@name", "Herman Melville")
}
});
However, with our requirements becoming more complex, we need the query to look different depending on given parameters. We also have "in"-queries that require us to add multiple parameters.
So now our code looks like this...
var sqlParameterCollection = new SqlParameterCollection();
for (int i = 0; i < ids.Length; i++)
{
var key = "@myid" + i;
sqlParameterCollection.Add(new SqlParameter(key, ids[i]));
}
[...]
var query = $@"
{select}
FROM collection m
WHERE m.myid IN ({string.Join(",", sqlParameterCollection.Select(p => p.Name))})
";
Next, the where clause will need to be extended with an additional filter depending on some parameters
Since this is getting worse and worse: Are there any query builders available for this? I am thinking about a fluent api that could ideally also include the SqlParameters, not only the query text.
Pseudo code:
queryBuilder
.from("m")
.select("field1")
.select("field2")
.where("myid", Operators.In, ...)
.And(...