My query returns data but it is not being added to my class

I have no difficulty adding records to my database using the class I created

    public class KElement
        public string Key { get; set; }  //created with a custom method using timestamp and internal company codes
        public string Type { get; set; } //currently hard-coded as equipment
        public string BarCode { get; set; }  //code 39
        public string ItemName { get; set; } 
        public string Manufacturer { get; set; }

According the the documentation it appears I should be able to use a QueryAsync method to return the data, then add an await foreach to add it to my class. Based on the documentation I wrote:

private async void BtnGetRecords_Click(object sender, EventArgs e)
            KElement Element = new KElement();
            var cluster = await Cluster.ConnectAsync("", "userName", "userPassword");

            var query = @"SELECT * FROM `warehouse` WHERE manufacturer = $1";
            var queryResult = await cluster.QueryAsync<dynamic>(query, options => options.Parameter("Monoprice")
            );  //if I put at break point here and hover over queryResult, it shows the correct data was returned and three records are present.

            await foreach ( var record in queryResult) //taken from the documentation
                Element.Type = record.type;  //returns null so nothing is added to my KElement class.
                Element.BarCode = record.barCode;  //returns null so nothing is added to my KElement class.
                Element.Manufacturer = record.manufacturer;  //returns null so nothing is added to my KElement class.
                Element.ItemName = record.itemName;  //returns null so nothing is added to my KElement class.

               RecordHandler(); //method to put display data on user screen


            await cluster.DisposeAsync();

Can anyone tell me where Iā€™m going wrong?

The most likely problem is the way N1QL returns the data. When using SELECT * it will nest every extent of the query within a property, which is unlike traditional SQL which returns the data flattened across all extents. Try running the query as SELECT warehouse.* FROM warehouse WHERE manufacturer = $1 instead. That will move the extent to the top level.

Note that you should also be able to query KElement directly without the dynamic intermediate step using cluster.QueryAsync<KElement>(...)


Thanks Brant. That indeed was the problem! Thanks again for the help

1 Like