1

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. I try to use below code but I need to return list of type 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

1 Answer 1

2

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. 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();            
}

Also, to only return the class4 types you can do the following:

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;       
}
Sign up to request clarification or add additional context in comments.

4 Comments

Great!! how can I convert the return type to List<Class4>?
you want to combine all list 4 elements?
Or only the first list4 you encounter?
Yes, I want to gather all Class4 types of all Class3 types

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.