Couchbase lite LINQ Support

I have started to use the Couchbase lite 1.4 , which has the support of LINQ . I am a dot net user .

I have written a simple LINQ query:
var result = from row in _db.AsQueryable(“1”)
where (row.DocumentProperties[“Uid”].Equals(site.Uid.ToString()))
select row.DocumentId;
I keep adding documents to the database . I just want to get the document ID of the newly created document . everytime the result must be a single document id .

I have a problem here

  1. I add a document run this query i get the expected resut for the first run
  2. I add another document run this query i get 2 document ID’s , whereas i must get only the latest added document id .
  3. every time i keep adding documents and running the query i cumulatively keep getting one extra document id’s everytime , whereas i must always get a single document ID

That LINQ query is doing what it is supposed to do by returning all results. It is a SELECT query just like in SQL. However, I think you are going to find that it is hard to get the ID of the “newly created” document, since that kind of query will be ordered lexographically by document ID. There are easier ways to get the document ID, but I think you might be just experimenting with LINQ. If you need just one result, there are various LINQ methods to help with that (i.e. Take Skip First Last, etc)

Can the variable in the Where clause of the LINQ Query be a variable entity of it has to be always fixed value .

    public T GetLinqString(string id1)
        var result = (from row in Db.AsQueryable("2")
                     where (stringConvert(row.DocumentProperties["Name"]) == (id1))
                     select row.DocumentProperties["Name"]).ToList();

or the entity in where clause be a fixed parameter like below

    public T GetLinqString(string id1)
        var result = (from row in Db.AsQueryable("2")
                     where (stringConvert(row.DocumentProperties["Name"]) == (label806039335))
                     select row.DocumentProperties["Name"]).ToList();

Because when i keep the parameter as a variable entity i am getting wrong results , whereas if i keep the entity in the where clause as constant i am getting correct results

I need to have a variable which changes everytime , i have requirement that i create a function which evaluates the variable passed to it and gives a result which satisfies the requirement in the WHERE clause

Good point this is something I failed to write in the wiki. The where clause will be subject to the same restrictions as map / reduce functions in that they must be pure and not rely on external state (I.e. For a given set of document properties it must produce the same results every time)

Ok , so how can i acheive my use case using LINQ . My use case is as below :slight_smile:

  1. I Keep on adding JSON documents to my database which has a parameter by name Label
  2. I need to create a function which takes a input parameter as a Label (string type) , the function Runs a LINQ query on that input parameter and must return the document which contains that label which was passed as an input parameter .

One more thing , how LINQ will be more beneficial then map reduce

It’s not meant to be an alternative to map / reduce, but rather an easier way to use map reduce since it is more familiar. In 2.0 it will be an actual query based engine though instead of a wrapper around map/ reduce though. For your case it might be easier to just set the document ID to be the name if it is guaranteed to be unique. Then you don’t need to make a query at all but rather just get the document by its ID.

Can we select more then one parameter in the Select Statement of the LINQ Query . If so how can i do it

Is It possible to select more then one parameters in the SELECT Statement of the LINQ query , if so please tell how can it be done

You cannot select more than one value in a select statement because that is how select statements work. However, you might consider selecting an array (i.e. select new[] { object1, object2 }).

I am doing the below as per your suggestion

         var result2 =(from xc in Db.AsQueryable()
                      where xc.DocumentProperties.ContainsKey("Name")
                      select new [] { obj1, obj2 });

But i get a run time access violation saying “Specified Cast is not Valid”

what is the rite way to return an array of objects

The query is like below

var result2 = (from xc in Db.AsQueryable()

where xc.DocumentProperties.ContainsKey(“Name”)

select new [] { xc.DocumentProperties[“Name”], xc.DocumentProperties[“Uid”] });

xc.DocumentProperties[“Name”] are the objects of my documents.

But i get a run time access violation saying “Specified Cast is not Valid”

Interesting. LINQ implementation is a gigantic puzzle of abstraction so I’ll have to look into this to see what is going on.

Thanks , but please let me know if there are any alternate options to achieve this , if not then we will have to wait for a full fledged query engine in 2.0

Any Help on this ?
LINQ Support in Couchbase Lite 1.4 version ?
Any workarounds to acheive my problem statement as above

So far I have not found a workaround (other than using map / reduce instead). I am looking into solving the problem today.

JSON .NET has complicated things for this particular case because of the JToken types that end up being created on selection. I’ve been able to fix this though. This fix will be available in the next CI build

Thanks for the fix , i need to know how can i use the next CI Build (1.4.1) . I Use C# (Microsoft Visual Studio 2017) , will it be available as a update in the Neuget Package manager .

Also please let me know by when it will be available . Thanks once again

You will need to set up a local package source (any folder on your disk will do) as in this picture. Then you can add in the Nuget packages from here. Note that these are largely untested (in other words, it’s possible that this fix has broken something somewhere else). Give it a try though. Any version above 62 will do it.