How to retrieve a couchbase document by passing its ID using Linq

@nithisha

Try this LINQ query and see if that works for you:

var beers = from b in context.Query<Beer>()
    where b.Type == "beer"
    select new {beer = b, id = N1QlFunctions.Meta(b).Id};

Also, you should note that we do offer some integrated, behind-the-scenes support for this if you use the experimental new change tracking feature in version 1.1. Assuming you are using the ID to perform updates, you can do this:

context.BeginChangeTracking();

var beers = from b in context.Query<Beer>()
    where b.Type == "beer"
    select b;

foreach (var beer in beers) {
  // modify the document
}

context.SubmitChanges();

In this case, the ID is automatically tracked internally, so that you don’t have to. However there are some POCO requirements. All of this is documented here:

Please let me know if you have more questions.

Brant

2 Likes