Reaching an array inside a MongoDB Collection

Jfisher387

Member
Joined
Dec 14, 2022
Messages
21
Programming Experience
Beginner
Hello,

I have a mongoDB collection of orders. Inside each order is a list of items that need purchased. I successfully have saved my orders to a collection. Now I would like to browse through my orders and view the contents of each order.I would of guessed I was able to access the list inside of toolList but doesn't seem to work as expected.

Here is what I have currently.

C#:
       private void ordersPage_Load(object sender, System.EventArgs e)
        {
            Owner.Hide();
            //ResizeColumns();
            List<Order> list = ordersCollection.AsQueryable().ToList();
            orderListDGV.DataSource = list;
        }

        private void orderListDGV_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = orderListDGV.CurrentCell.RowIndex;
            string text = Convert.ToString(orderListDGV[1, index].Value);
            var filter = Builders<Order>.Filter.Eq("Po", text);
            var toolList = ordersCollection.Find(filter).ToList<Order>();
            currentOrder.DataSource = toolList;
        }

And my classes that structure the data
C#:
 public class OrderItem
    {

        [BsonElement("Type")]
        public string Type { get; set; }

        [BsonElement("Vendor")]
        public string Vendor { get; set; }

        [BsonElement("Description")]
        public string Description { get; set; }

        [BsonElement("Cost")]
        public double Cost { get; set; }

        [BsonElement("Order Qty")]
        public int OrderQty { get; set; }

        [BsonElement("Serial")]
        public int Serial { get; set; }

        [BsonElement("PID")]
        public string PID { get; set; }
    }
    public class Order
    {

        public Order(List<OrderItem> listOfItems, string po, double orderTotal, DateTime timeOfOrder)
        {
            OrderItemList = listOfItems;
            Po = po;
            OrderTotal = orderTotal;
            OrderDate = timeOfOrder;
        }
        public ObjectId Id { get; set; }

        [BsonElement("Order")]
        public List<OrderItem> OrderItemList { get; set; }

        [BsonElement("PO")]
        public string Po { get; set; }

        [BsonElement("Order Total")]
        public double OrderTotal { get; set; }
        

        [BsonElement("Date")]
        public DateTime OrderDate { get; set; }


    }
 
access the list inside of toolList but doesn't seem to work as expected.
What behavior are you seeing? What behavior were you expecting?: What have you researched to try to resolve the issue?
 
I'm just unsure of the logic how to access the array inside of a list.

I know that the collection has a list of orders. and each item on that list has a list of items. each of those items have properties that i want to access, and display.

what im unsure of is the syntax.
 
It's because you are trying to pack too much in your line of code trying to emulate the fluent style with your:
C#:
ordersCollection.Find(filter).ToList<Order>();

If you did it step by step:
C#:
var found = ordersCollection.Find(filter);
var filteredOrdersList = found.ToList<Order>();
and continue on with:
C#:
Order firstOrder = filtereredOrderList.First();
List<OrderItem> toolsList = firstOrder.OrderItemList;
then you can assign that toolsList as the DataSource for your data grid view.
 

Latest posts

Back
Top Bottom