简体   繁体   中英

How i can add Couchbase Documents in a list?

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' takes 2' 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 type Demo_Couchbase.Visitor' (CS1503) (Demo_Couchbase.Droid)

Thank you in advance for any help you can provide.

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;
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM