question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUERY] How to return a single result from query or null

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
christothescommented, Nov 24, 2021

Though the solution provided was satisfactory, I would like to add two use cases when this may not fully work:

  • The caller may not have details about the PartitionKey or RowKey details when calling this (in my case through a library). The uniqueness is provided through the predicate call. But a predicate seem not to be an option for this call.

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.

  • I would still have preferred a solution to the LINQ’s IQueryable FirstOrDefault(). It would be more in sync with the common APIs for such calls and also fulfill the use case when, the query may or may not return one or more items but you only one the first on top of the stack, and you want to do that without bringing all records all the way to the client to inspect them (which is what I am doing now).

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.

var pages = table.QueryAsync<TableEntity>(t => <some filter>, maxPerPage: 1).AsPages();
await foreach (var tableEntity in pages)
{
	// Do something with the first entitiy here.
	break;
}
0reactions
hgjuracommented, Nov 24, 2021

Good workaround. Thanks.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found