[QUERY] How to return a single result from query or null
See original GitHub issueI am looking for a way to run queries that return a single record. Something of the type of .FirstOrDefault()
Currently I have this working like this:
var tsc = new TableServiceClient(new Uri($"https://{AccountName}.table.core.windows.net/"), new TableSharedKeyCredential(AccountName, AccountKey));
var tc = tsc.GetTableClient("SOMETABLE");
SomeEntity r = null;
var q = tcQueryAsync<SomeEntity>(filter: e => e.PartitionKey == "some partition key" && e.RowKey == "some row key"); //<-- THSI RETURNS A SINGLE RECORD, ALWAYS
await foreach (var i in q) r ??= i;
return r;
Is there a more efficient way to return, or look for, a single query result. The way I have looked into the samples is always expected multiple results.
Thanks Herald
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
SQL server return only single row or null
I have a SQL query which I only ever want to return 1 row. If there are multiple rows, I want to return...
Read more >How to return Null or value when nothing is ...
I have a simple query: SELECT name FROM atable WHERE a = 1. I want it to return Null if it finds nothing,...
Read more >Why does my SELECT query not return null values?
SELECT * FROM the_values WHERE value IN (1,2,3,4,5,6,10) IS NOT TRUE;. Or: .. WHERE value = ANY('{1,2,3 ...
Read more >Returning a NULL Value when query returns nothing
I know that in theory, the query should return nothing. However, I am wanting to copy and paste the results into an excel...
Read more >Working with SQL NULL values
In the following query, the COALESCE() function returns the SQLShack.com because it is the first non-null value in the list.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The only valid unique predicate for a guaranteed single record result is to specify both the PartitionKey and RowKey values. Anything else is a standard Query which can have zero to many results.
Performing a server side “SingleOrDefault” is not possible with the Table service, so the only alternative is to perform a query with a
maxPerPage
option of 1 and iterate only through the first page of results. Below is an example of how you’d need to fetch just a single record this way.Good workaround. Thanks.