Add Object to BindingList in BindingList

I have an activity binding list, and each action has a BuyOrders binding list

bindingListActivty.Select(k => k._dataGridViewId == 1); 

If I understand correctly, I can select an action, but I can’t access any method inside the action. How to access the method without creating a new instance of the list of bindings?

I would have worked, but not

 bindingListActivty.Select(k => k._dataGridViewId == 1).addBuyOrders(new BuyOrders()); 
+5
source share
3 answers

You can try the following:

 // Get the activity from bindingListActivity, whose k._dataGridViewId is equal to 1. var activity = bindingListActivty.SingleOrDefault(k => k._dataGridViewId == 1); // If the activity has been found and the a new BuyOrders object. if(activity!=null) activity.addBuyOrders(new BuyOrders()); 
+3
source

The selection returns an IEnumerable<T> , which your addBuyOrders method will not have. You need to either use foreach or use FirstOrDefault with the Where clause to get a separate object that the method provides.

For instance:

Eogeasp:

 var activities = bindingListActivty.Select(k => k._dataGridViewId == 1); foreach(var a in activities) { a.addBuyOrders(new BuyOrders()); } 

FirstOrDefault (this probably makes more sense based on your where clause):

 var activity = bindingListActivty.Where(k => k._dataGridViewId == 1).FirstOrDefault(); if (activity != null) { activity.addBuyOrders(new BuyOrders()); } 
+5
source

It's important for you to understand that IEnumerable<T>.Select() not for queries. For any queries, you must use the Where (), First (), or FirstOrDefault () functions. Select () is the projection of each item. This means that you are doing the conversion from T1 β†’ T2. You projected each action onto a logical value ( k._dataGridViewId == 1 ). Result Type

 bindingListActivty.Select(k => k._dataGridViewId == 1); 

is an

 IEnumerable<bool> 
+2
source

Source: https://habr.com/ru/post/1203301/


All Articles