简体   繁体   English

使用 C# Mongodb 驱动程序在嵌套列表中查找项目

[英]Find items in nested list using C# Mongodb driver

I have a model like this:我有一个这样的模型:

Class1 
{
    public List<Class2> Class2List { get; set; }
}

Class2 
{
    public List<Class3> Class3List { get; set; }
}

Class3 
{
    public List<Class4> Class4List { get; set; }
}

Class4 
{
    public int Value { get; set; }
    public DateTime CreateDate{ get; set; }
}

I need to find a list of items of Class4 which are between two dates.我需要找到两个日期之间的 Class4 项目列表。 I try to use below code but I need to return list of type Class4:我尝试使用下面的代码,但我需要返回 Class4 类型的列表:

public async Task<IList<Class1>> GetClass4BetweenDates(string Id, DateTime fromDate, DateTime toDate)
{
    var builder = Builders<Class1>.Filter;
    var filter = builder.Eq("Id", Id) & builder.Gte("Class2.Class3.Class4.CreateDate", fromDate) & builder.Lte("Class2.Class3.Class4.CreateDate", toDate);
    return await _context.GetCollection<Class1>().Find(filter).ToListAsync();            
}

I know this is wrong.我知道这是错误的。 I need to know the correct way我需要知道正确的方法

I think you should try this:我认为你应该试试这个:

The only problem is in your example that you only give a create date witch elements should be bigger then and smaller then the CreateDate.唯一的问题是在您的示例中,您只给出了一个创建日期,女巫元素应该大于然后小于 CreateDate。 This is kind off imposible.这有点不可能。

public async Task<IList<Class1>> GetClass4BetweenDates(string id, DateTime fromDate, DateTime toDate)
{
    var filterDefinition = Builders<Class1>.Filter.Where(x => x.Id == id &&
                                                              x.Class2List.Any(a => a.Class3List.Any(b => b.Class4List.Any(c => c.CreateDate >= fromDate && 
                                                                                                                                c.CreateDate <= toDate))));
    return await _context.GetCollection<Class1>().Find(filterDefinition).ToListAsync();            
}

To the follow up question to only return the class4 types you can do the following:对于仅返回 class4 类型的后续问题,您可以执行以下操作:

public async Task<IList<Class4>> GetClass4BetweenDates(string id, DateTime fromDate, DateTime toDate)
{
    var filterDefinition = Builders<Class1>.Filter.Where(x => x.Id == id &&
                                                              x.Class2List.Any(a => a.Class3List.Any(b => b.Class4List.Any(c => c.CreateDate >= fromDate && 
                                                                                                                                c.CreateDate <= toDate))));
    var class1 = await _context.GetCollection<Class1>().Find(filterDefinition).ToListAsync();  

    var class4List = new List<Class4>();

    if (class1 == null)
        return class4List; 

    foreach (var class2 in class1.Class2List)
    {
        foreach (var class3 in class2.Class3List)
        {
            if (class3.Class4List.Any())
                class4List.AddRange(class3.Class4List);
        }
    }

    return class4List;       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 mongodb .net 驱动程序 C# 在集合中查找嵌套数组字段包含特定字符串的项目? - How to find items in a collection that nested array field contains specific string using mongodb .net driver C#? C# MongoDB 驱动程序 - 如何按 arrays 和项目列表进行过滤 - C# MongoDB Driver - How to filter by arrays and a list of items 使用官方C#MongoDB驱动程序更新嵌套列表中的单个元素 - Updating a single element in a nested list with official C# MongoDB driver 如何使用 MongoDB C# 驱动程序投影查询嵌套列表 - How to query nested list with MongoDB C# Driver projections 使用 C# 驱动更新 MongoDB 中的多项数组 - Update multi items of array in MongoDB using C# Driver 如何使用C#驱动程序在mongodb嵌套数组中搜索 - How to search in mongodb nested array using c# driver 使用C#驱动程序反序列化嵌套类时,MongoDB中出现错误 - Error in MongoDB when using C# driver to deserialize a nested class 使用 c# Mongodb 驱动程序查询和投影嵌套数组 - query and project a nested array using the c# Mongodb driver 插入嵌套数组使用C#Official Driver MongoDB - Insert in to a nested array Using C# Official Driver MongoDB MongoDB 驱动程序 C#,使用过滤器定义按嵌套属性搜索 - MongoDB Driver C#, Search by nested property using filter definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM