MongoDB通过C#查询嵌入式文档数组

时间:2019-03-08 11:36:11

标签: c# mongodb linq lambda

有一个文档,其中定位了导航菜单和菜单项。 从查询中我什么也没得到。因为它总是带来主菜单的所有元素。第一个Find()方法很好,但是我为嵌入式文档创建的Any()方法没有给出我想要的结果。

public class NavMenu
{
    public string Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public AuthList Auths { get; set; }
    public List<NavMenuItems> MenuItems { get; set; }
}

public class AuthList
{
    public bool Program { get; set; }
    public bool Admin { get; set; }
    public bool Moderator { get; set; }
    public bool Member { get; set; }
    public bool AnyBody { get; set; }
}

public class NavMenuItems
{
    public string Id { get; set; }
    public string MenuId { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public bool IsActive { get; set; }
    public AuthList Auths { get; set; }
}
List<NavMenu> menuList = await collection.Find(m =>
                           m.IsActive && m.Auths.Program &&
                           m.MenuItems.Any(i => i.IsActive && i.Auths.Program)).ToListAsync();

编辑:上面的查询将菜单1和其中的所有菜单项带给我。但是我想要Menu1及其子项Menu Item 1和Menu Item 2

List<NavMenu> navMenus = new List<NavMenu>();
        List<NavMenuItems> menuItems = new List<NavMenuItems>();
        menuItems.Add(new NavMenuItems
        {
            Name = "MenuItem 1",
            Auths= new AuthList {Admin = false, AnyBody = false, Member = false, Moderator = false, Program = true},
            IsActive = true,
            MenuId = "1",
            Url = "someUrl"
        });
        menuItems.Add(new NavMenuItems
        {
            Name = "MenuItem 2",
            Auths= new AuthList {Admin = false, AnyBody = false, Member = false, Moderator = false, Program = true},
            IsActive = true,
            MenuId = "1",
            Url = "someUrl"
        });
        menuItems.Add(new NavMenuItems
        {
            Name = "MenuItem 3",
            Auths= new AuthList {Admin = true, AnyBody = false, Member = false, Moderator = false, Program = false},
            IsActive = true,
            MenuId = "1",
            Url = "someUrl"
        });
        menuItems.Add(new NavMenuItems
        {
            Name = "MenuItem 4",
            Auths= new AuthList {Admin = false, AnyBody = false, Member = false, Moderator = false, Program = true},
            IsActive = false,
            MenuId = "1",
            Url = "someUrl"
        });
        navMenus.Add(new NavMenu
        {
            Id = "1",
            Auths = new AuthList {Admin = false, AnyBody = false, Member = false, Moderator = false, Program = true},
            IsActive = true, Name = "Menu 1",
            MenuItems = menuItems
        });

0 个答案:

没有答案