Convert Keyvalue pair to List<> object in C#

what is the syntax to convert KeyValue pair onto a list object in C#?

public List<Profile> GetAll()
{
    var request = QueryRequest.Create("SELECT META().id AS `key`, TOOBJECT(hc) as `value` FROM `test` as hc WHERE type='Profile';");
    request.ScanConsistency(ScanConsistency.RequestPlus);
    var response = _bucket.Query<KeyValuePair<string, Profile>>(request);
    Dictionary<string, Profile> temp= response.Rows.ToDictionary(x => x.Key, x => x.Value);

    List<Profile> prfs1 = new List<Profile>();
    foreach (KeyValuePair<string, Profile> entry in temp)
    {
            foreach (string item in (entry.Value as JArray).ToObject<string[]>())
            prfs.Add(item);
    }
    return prfs1 ;
}

Hi @rkbnair

I fixed up your code sample and moved the post to the .NET section.

If you just want to a list of all the values in a response, you could project just the values like this:

var response = _bucket.Query<KeyValuePair<string, Profile>>(request);
var profiles = response.Rows.Select(x => x.Value).ToList();

However, as you’re not using the ‘key’ field, you could make your query simpler and therefore your QueryResult type like this:

public List<Profile> GetAll()
{
    var request = QueryRequest.Create("SELECT TOOBJECT(hc) as `value` FROM `test` as hc WHERE type='Profile';");
    request.ScanConsistency(ScanConsistency.RequestPlus);
    var response = _bucket.Query<Profile>(request);
    return response.Rows.ToList();
}

Thanks

1 Like