0

I'm experimenting with Couchbase + Xamarin.Forms trying to do a simple search, showing the results in a ListView but I've stuck. :( Someone know how to add the rows/documents of a query in a list?

public List<Visitor> SearchRecord (string word)
{
   var viewByName = db.GetView ("ByName");
   viewByName.SetMap((doc, emit) => {
     emit (new object[] {doc["first_name"], doc["last_name"]}, doc);
   }, "2");

   var visitorQuery = viewByName.CreateQuery();

   visitorQuery.StartKey = new List<object> {word};
   // visitorQuery.EndKey = new List<object> {word, new Dictionary<string, object>()};
   visitorQuery.Limit = 100;

   var visitors = visitorQuery.Run();
   var visitorList = new List<Visitor> ();

   foreach (var visitor in visitors) {
     // visitorList.Add(visitor.Document); <-- Error.
     System.Console.WriteLine(visitor.Key);
   }

   return visitorList;
}

I get the error messages:

Error CS1501: No overload for method Add' takes2' arguments (CS1501) (Demo_Couchbase.Droid) Error CS1502: The best overloaded method match for System.Collections.Generic.List<Demo_Couchbase.Visitor>.Add(Demo_Couchbase.Visitor)' has some invalid arguments (CS1502) (RegistroAgil_Couchbase.Droid) Error CS1503: Argument#1' cannot convert Couchbase.Lite.Document' expression to typeDemo_Couchbase.Visitor' (CS1503) (Demo_Couchbase.Droid)

Thank you in advance for any help you can provide.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jonathan Zúñiga
  • 645
  • 3
  • 13
  • 25

2 Answers2

0

There is problem in your mapping part. You can directly cast documents on GetView. You can try bellow code.

 public List<Visitor> SearchRecord (string word)
{
   var viewByName = db.GetView<Visitor>("ByName","ByName");
   var visitorQuery = viewByName.CreateQuery();
   visitorQuery.StartKey = new List<object> {word};
   visitorQuery.Limit = 100;
   var visitors = visitorQuery.Run();
   var visitorList = new List<Visitor> ();

   foreach (var visitor in visitors) {
     visitorList.Add(visitor.Document); 
     System.Console.WriteLine(visitor.Key);
   }
   return visitorList;
}
Sudharshan
  • 348
  • 1
  • 9
  • It throws me an error: Error CS0308: The non-generic method `Couchbase.Lite.Database.GetView(string)' cannot be used with the type arguments (CS0308) (Demo_Couchbase.Droid) – Jonathan Zúñiga Apr 10 '15 at 15:55
  • @JonathanZúñiga, var viewByName = db.GetView("ByName","ByName"); You need to pass two arguments – Sudharshan Apr 16 '15 at 06:00
  • 1
    Hi, with the two arguments it gives me this error: No overload for method `GetView' takes `2' arguments (CS1501). Thank you Sudharshan, but i have solved my problem and i will post what i have done. – Jonathan Zúñiga May 12 '15 at 20:28
0

I don't know if this is the most elegant solution though but my code works fine now.

Visitor ToRecord(Document d) {
    var props = d.Properties;

    return new Visitor {
        Id = props["_id"].ToString(),
        FirstName = (string)props["first_name"],
        LastName = (string)props["last_name"],
        Occupation = (string)props["occupation"],
        Company = (string)props["company"],
        Email = (string)props["email"],
        Phone = (string)props["phone"],
        Birthday = (string)props["birthday"],
        LastVisit = (string)props["last_visit"],
        LocalImagePath = (string)props["local_image_path"],
        Type = (string)props["type"],
        CreatedAt = (string)props["created_at"],
        UpdatedAt = (string)props["updated_at"],
        DeletedAt = (string)props["deleted_at"]
    };
}

public List<Visitor> SearchRecord (string word)
{
    var viewByName = db.GetView ("ByName");

    viewByName.SetMap((doc, emit) => {
        if ((doc.ContainsKey("type") && doc["type"].ToString() == "visitor") && (doc.ContainsKey("deleted_at") && doc["deleted_at"] == null))
                emit (new [] {doc["first_name"], doc["last_name"]}, doc);
    }, "2");

    var visitorQuery = viewByName.CreateQuery();

    visitorQuery.StartKey = word;
    visitorQuery.Limit = 50;

    var rows = visitorQuery.Run();
    var visitorList = new List<Visitor> ();

    for (int i = 0; i < rows.Count (); i++) {
        var row = rows.GetRow (i);
        var name = row.Document.GetProperty ("first_name").ToString ().ToLower () + " " + row.Document.GetProperty ("last_name").ToString ().ToLower ();

        if (name.Contains (word))
            visitorList.Add(ToRecord(row.Document));
    }

    return visitorList;
}
Jonathan Zúñiga
  • 645
  • 3
  • 13
  • 25
  • Welcome to StackOverflow! When posting an answer please include more than just a code snippet - identify what the solution was so others can benefit from your solution too. – dimo414 May 12 '15 at 20:59