Couchbase Lite 2.0, Query from DB21 to Db22, How to?

Hi,

With the version DB21, Couchbase 2.0, Xamarin Forms, C#, I used to write a query like this example :

IQuery query = Query.Select(SelectResult.All())
.From(DataSource.Database(_dbConfigurationGetter.Get()))
.Where(Expression.Property("table").EqualTo("server"));
var rows = query.Execute();
if (rows.Count == 0)
	return null
return rows.Select(r => r.GetDictionary(0).ToMutable().ToServer()).FirstOrDefault();

(FirstOrDefault is just because this example is from my configuration section, And I expect just 0 or 1 doc with the property table = “server”)

I did see in the changelog that a Query became a QueryBuilder, so I did it like that :

IQuery query = QueryBuilder.Select(SelectResult.All())
			.From(DataSource.Database(_dbConfigurationGetter.Get()))
			.Where(Expression.Property("table").EqualTo(Expression.String("server")));


		var rows = query.Execute();
		if (rows.Count() == 0)
			return null;

		return rows.Select(r => r.GetDictionary(0).ToMutable().ToServer()).FirstOrDefault();

I have this exception :

This result set has already been enumerated, please re-run Execute() on the original query

And this stacktrace :

   at Couchbase.Lite.Internal.Query.QueryResultSet.<GetEnumerator>b__18_0()
   at Couchbase.Lite.Support.ThreadSafety.DoLocked(Action a)
   at Couchbase.Lite.Internal.Query.QueryResultSet.GetEnumerator()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at mobiLotis.Configuration.ServerDal.Get()
   at mobiLotis.Services.ReplicatorGetter..ctor(IDataBaseGetter dataBaseGetter, IServerDal serverDal, ISynchroExDal synchroExDal)
   at lambda_method(Closure )
   at SimpleInjector.Lifestyles.SingletonLifestyle.SingletonLifestyleRegistration`1.CreateInstanceWithNullCheck()
   at SimpleInjector.Lifestyles.SingletonLifestyle.SingletonLifestyleRegistration`1.GetInterceptedInstance()
   at SimpleInjector.Lifestyles.SingletonLifestyle.SingletonLifestyleRegistration`1.BuildExpression()
   at SimpleInjector.InstanceProducer.BuildExpressionInternal()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)

What did I made wrong ?

Regards

Steeve

In db21 and before the Count used to be a property, but now it’s a method which enumerates the collection. So when you do rows.Count() (or rather !rows.Any()) you have to re-run the query to get a new resultset.

Alternatively you can call ToList() first before calling Count and Select.

Hi,

Ok, Your 2 solutions are working.

Thank you guys !

Steeve