I want to return single value from an ASP.NET Core 8 Web API method:
public async Task<Driver> GetDriverByIdAsync(string id)
{
var queryDefinition = new QueryDefinition($"SELECT * FROM c WHERE c.id = '{id}'");
var queryResult = await _container.GetItemQueryIterator<Driver>(queryDefinition).ReadNextAsync();
return queryResult.FirstOrDefault();
}
This method works fine and returns all data by provided id, but I want a single property value like name
.
I tried this:
public async Task<string> GetDriverByIdAsync2(string id)
{
var queryDefinition = new QueryDefinition($"SELECT c.name FROM c WHERE c.id = '{id}'");
var queryResult = await _container.GetItemQueryIterator<string>(queryDefinition).ReadNextAsync();
return queryResult.FirstOrDefault();
}
but it does not work.
How should I correct this code for it to return only a single property value?
What is the right method to make it return only a single property value?
Have you tried something like this:
You are creating an object (Item) with a matching property (name). For GetItemQueryIterator you are using that data type to bind on.
public class Item
{
public string name {get; set;}
}
public async Task<string> GetDriverByIdAsync2(string id)
{
var queryDefinition = new QueryDefinition($"SELECT c.name FROM c WHERE c.id = '{id}'");
var queryResult = await _container.GetItemQueryIterator<Item>(queryDefinition).ReadNextAsync();
return queryResult.FirstOrDefault().name;
}