I’ve started using the plugin: Couchbase.Lite.Mapping
for binding my model and database documents together. I really appreciate the effort put into this by Rob Hedgpeth. However, I have a challenge trying to put a “group by” query’s results into a list of models with just a couple of properties.
Not sure if this is the best place to ask - but thought there is a good “concentration” of Couchbase people here - so someon may know.
So this is my generic GetList
method with special handling for model DataEntity
:
var list = new List<T>();
// Special handling for DataEntity
if (string.Equals(typeof(DataEntity).Name, typeof(T).Name))
{
using (var query = QueryBuilder.Select(
SelectResult.Expression(Function.Count(Expression.All())).As("count"),
SelectResult.Property("type"))
.From(dbSource)
.Where(Expression.Property("type").NotNullOrMissing())
.GroupBy(Expression.Property("type"))
.OrderBy(Ordering.Property("type").Ascending()))
{
var result = query?.Execute()?.AllResults();
// This works - but does not using the mapping module....
foreach (var record in result)
{
var d = new DataEntity();
d.Count = record.GetInt(COUNT);
d.Type = record.GetString(TYPE);
list.Add((T)(object)d);
}
// This, however, crashes the app with
// Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: Null.
//var items = result?.ToObjects<T>(AppConstants.DB_NAME);
// This crashes with an error that key cannot be null
//var items = result?.ToObjects<T>(null);
//list.AddRange(items);
}
}
:
I guess the problem lies in the fact that there is no “root” element (being the bucket name).
Does anyone know if I somehow can use the mapping plugin to get results like this into a list of objects instead of having to do it manually?