ravendb查询子列表计数= 0

时间:2014-10-29 14:21:16

标签: ravendb

我在ravendb中有一个名为documents的对象。这用于存储文件的位置和详细信息。

这是对象的类视图:

public class Document
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public string Htmlform { get; set; }
    public Client Client { get; set; }
    public IList<Folder> Folders { get; set; }
    public IList<Team> Teams { get; set; }
    public IList<Users> Users { get; set; }
    public Users CreatedBy { get; set; }
    public Users LastChangedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime LastChanged { get; set; }
    public IList<DocumentElement> DocumentElements { get; set; }
    public MimeType MimeType { get; set; }
    public DocumentState State { get; set; }
}

在我的用户界面中,当选择一个文件夹时,我想获取仅分配给该文件夹的文件,因为视图将团队显示为文件夹,而内部则是带有团队的用户的文件夹。

所以我构建了一个查询,试图获取文件夹所在的结果(选定的文件夹),但团队和用户是未分配的。

var inFolder = await Session.Query<Document>()
                        .Where(
                            x =>
                                x.Folders.Any(a => a.Id == id) && x.Teams.Count() == 0 && x.Users.Count() == 0 &&
                                x.State.Name.StartsWith("Pub")).ToListAsync();

然而,这会产生0条记录,但我有这条记录:

{
  "Name": "logo@1x.png",
  "Location": "https://removed.blob.core.windows.net/removed/fffa6220-69a4-48fa-9537-09006666a287.png",
  "Htmlform": null,
  "Client": {
    "Id": "Client-06ec8641-e206-41b4-b211-7c2bce2b9f33",
    "Name": "removed",
    "ClientEnabled": true
  },
  "Folders": [
    {
      "Id": "Folder-4e7eabc5-32e7-4d00-9800-4327aaac823f",
      "Name": "Testing",
      "DocumentType": {
        "Id": "DocumentType-f6e732d0-4fde-4956-981a-26e3d5397661",
        "Name": "General Documents",
        "Client": {
          "Id": "Client-06ec8641-e206-41b4-b211-7c2bce2b9f33",
          "Name": "removed",
          "ClientEnabled": true
        }
      }
    }
  ],
  "Teams": [],  <-- empty as it should be
  "Users": [],  <-- empty as it should be
  "CreatedBy": { removed user object },
  "LastChangedBy": { removed user object },    
  "CreatedDate": "2014-10-29T00:00:00.0000000",
  "LastChanged": "2014-10-29T13:36:50.7311474",
  "DocumentElements": null,
  "MimeType": null,
  "State": {
    "Id": "DocumentState-b1b35556-acc3-4b07-b4fd-3bb7d6e764d1",
    "Name": "Published"
  }
}

我也试过!x.Teams.Any()

创建的索引

public Index_Auto_2fDocuments_2fByFolders_IdAndState_NameAndTeams_Count__AndUsers_Count__()
{
    this.ViewText = @"from doc in docs.Documents
    from docFoldersItem in ((IEnumerable<dynamic>)doc.Folders).DefaultIfEmpty()
     select new { Teams_Count__ = doc.Teams[""Count()""], Users_Count__ = doc.Users[""Count()""],         Folders_Id = docFoldersItem.Id, State_Name = doc.State.Name }";
    this.ForEntityNames.Add("Documents");
    this.AddMapDefinition(docs => 
        from doc in docs
        where string.Equals(doc["@metadata"]["Raven-Entity-Name"], "Documents",    System.StringComparison.InvariantCultureIgnoreCase)
        from docFoldersItem in ((IEnumerable<dynamic>)doc.Folders).DefaultIfEmpty()
        select new {
            Teams_Count__ = doc.Teams["Count()"],
            Users_Count__ = doc.Users["Count()"],
            Folders_Id = docFoldersItem.Id,
            State_Name = doc.State.Name,
            __document_id = doc.__document_id
        });
    this.AddField("Teams_Count__");
    this.AddField("Users_Count__");
    this.AddField("Folders_Id");
    this.AddField("State_Name");
    this.AddField("__document_id");
    this.AddQueryParameterForMap("Id");
    this.AddQueryParameterForMap("State.Name");
    this.AddQueryParameterForMap("__document_id");
    this.AddQueryParameterForReduce("Id");
    this.AddQueryParameterForReduce("State.Name");
    this.AddQueryParameterForReduce("__document_id");
}

所以有任何关于如何进行此类查询的想法,以便我可以获得一个文件的结果。感谢

1 个答案:

答案 0 :(得分:1)

最后,我已经解决了。

var inFolder = await Session.Query<Document>()
                        .Where(
                            x =>
                                x.Folders.Any(a => a.Id == id) && x.Teams.Any() == false && x.Users.Any() == false &&
                                x.State.Name.StartsWith("Pub")).ToListAsync();

我已经将查询更改为任何为false,在每个项目/对象上,这也使得更整洁的索引