如何在列表中添加Couchbase文档?

时间:2015-04-10 05:44:35

标签: c# .net xamarin couchbase couchbase-lite

我正在尝试使用Couchbase + Xamarin.Forms尝试进行简单搜索,在ListView中显示结果但我已经卡住了。 :( 有人知道如何在列表中添加查询的行/文档吗?

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

我收到错误消息:

  

错误CS1501:方法Add' takes 2'参数没有重载   (CS1501)(Demo_Couchbase.Droid)错误CS1502:最佳重载   方法匹配   System.Collections.Generic.List<Demo_Couchbase.Visitor>.Add(Demo_Couchbase.Visitor)' has some invalid arguments (CS1502) (RegistroAgil_Couchbase.Droid) Error CS1503: Argument#1'无法转换Couchbase.Lite.Document' expression to type Demo_Couchbase.Visitor'(CS1503)   (Demo_Couchbase.Droid)

提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

您的地图部分存在问题。您可以直接在GetView上投射文档。 您可以尝试以下代码。

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

答案 1 :(得分:0)

我不知道这是否是最优雅的解决方案,但我的代码现在工作正常。

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