I am trying run a Datastore query to get a list of names and prices. However, I keep getting this error message:
Cannot implicitly convert type '
Google.Cloud.Datastore.V1.DatastoreQueryResults
' to 'System.Collections.Generic.List<TestApp.Models.AllSportsStore>
'
This is the code I am using:
AllSportsStore.cs Page
public DatastoreDb _db;
[BindProperty]
public List<AllSportsStore> SportsStoreList { get; set; }
public void OnGet()
{
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xxxxx.json"));
_db = DatastoreDb.Create("projectid");
Query query = new Query("Sports_db");
IEnumerable<Entity> stores = _db.RunQuery(query).Entities;
SportsStoreList = stores.Select(_ => new AllSportsStore
{
Name = _["Name"].ToString(),
Price = _["Price"].ToString(),
}).ToList();
}
AllSportsStore.cshtml page
@for (var i = 0; i < Model.SportsStoreList.Count; i++)
{
<tr>
<td>
@Html.DisplayFor(model => model.SportsStoreList[i].Name)
</td>
<td>
@Html.DisplayFor(model => model.SportsStoreList[i].Price)
</td>
</tr>
}
This is the image of the datastore
Updated code result based on a comment
The Python Datastore API provides two classes for preparing and executing queries: Query uses method calls to prepare the query. GqlQuery uses a SQL-like query language called GQL to prepare the query from a query string.
For highly related or hierarchical data, Datastore allows entities to be stored in a parent/child relationship. This is known as an entity group or ancestor/descendent relationship. This is an example of an entity group with kinds of types person, pet, and toy. The 'Grandparent' in this relationship is the 'Person'.
Open "Datastore Admin" for your application and enable Admin. Then all of your entities will be listed with check boxes. You can simply select the unwanted entites and delete them.
As already stated in the comments, you are trying to assign the wrong type to the SportsStoreList
property.
Have a model to hold entity details from storage
public class SportsStoreItem {
public string Name { get; set; }
public decimal Price { get; set; }
}
Use the model in the AllSportsStore.cs Page
public class AllSportsStore : PageModel {
private readonly DatastoreDb _db;
public AllSportsStore() {
_db = DatastoreDb.Create("projectid");
}
[BindProperty]
public List<SportsStoreItem> SportsStoreList { get; set; }
public IActionResult OnGet() {
Query query = new Query("Sports_db");
IEnumerable<Entity> stores = _db.RunQuery(query).Entities;
SportsStoreList = stores.Select(_ => new SportsStoreItem {
Name = (string)_["Name"],
Price = (decimal)_["Price"]
}).ToList();
return Page();
}
}
Note how the entities retrieved from the data store db were converted to strongly typed objects.
You should then be able to access the items in the list in the view/page.
@for (var i = 0; i < Model.SportsStoreList.Count; i++) {
<tr>
<td>
@Html.DisplayFor(model => model.SportsStoreList[i].Name)
</td>
<td>
@Html.DisplayFor(model => model.SportsStoreList[i].Price)
</td>
</tr>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With