mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.35k stars 1.22k forks source link

Why Include() is not loading all nested items #2479

Open Josip84 opened 1 month ago

Josip84 commented 1 month ago

Here is my example:

static void Main(string[] args)
{
    Occupation occupation = new Occupation()
    {
        OccupationName = "Oc"
    };

    Names names = new Names()
    {
        Name = "a",
        LastName = "b",
        Occupation = occupation
    };

    List<Names> nameslist = new List<Names>();
    nameslist.Add(names);

    MyMainClass myMainClass = new MyMainClass()
    {
        ID = 1,
        Names = nameslist
    };

    using (var db = new LiteDatabase(@"MyData.db"))
    {
        var col = db.GetCollection<MyMainClass>(nameof(MyMainClass));

        col.EnsureIndex(x => x.ID, true);
        col.Insert(myMainClass);

        var r = col
            .Include(x => x.Names)
            .FindAll()
            .FirstOrDefault();

        Console.WriteLine(r);
    }
}
public class MyMainClass
{
    public int ID { get; set; }
    public List<Names> Names { get; set; }
}

public class Names
{
    public string Name { get; set; }
    public string LastName { get; set; }

    public Occupation Occupation { get; set; }
}

public class Occupation
{
    public string OccupationName { get; set; }
}

Running this only loading only Names but Occupation is null.

I also tried on this way

var r = col .Include(x => x.Names) .Include(x => x.Names.Select(n => n.Occupation)) .FindAll() .FirstOrDefault();