Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all elements from Lists inside a List

Tags:

c#

litedb

I have following data model:

    public class Customer
    {
        [BsonId]
        public int CustomerId { get; set; }
        public string Name { get; set; }
    }

    public class Order
    {
        [BsonId]
        public int OrderId { get; set; }
        [BsonRef("customers")] 
        public Customer Customer { get; set; }
    }

    public class Cart
    {
        [BsonId]
        public int CartID { get; set; }
        public List<Order> OrderList { get; set; }
    }

Now I want to access the customers via queries on my cart collection. I tried something like this:

var collectionCarts = db.GetCollection<Cart>("carts");
var result = collectionCarts.Include("OrderList.Customer").Find(x => x.OrderList[0].Customer.CustomerId == 2);

However, all attempts result in an empty customer object. My database, which I checked with the LiteDB-Viewer seems to be OK.

like image 541
h0ch5tr4355 Avatar asked Oct 19 '25 15:10

h0ch5tr4355


1 Answers

You could get the Customers like this:

using System.Linq;

collectionCarts.SelectMany(c => c.OrderList.Select(o => o.Customer));
like image 160
Romano Zumbé Avatar answered Oct 21 '25 03:10

Romano Zumbé