.net ライブラリには多くの優れたコレクションがあるため、このカスタム リストが必要な理由はよくわかりませんが、以下のことを試してみました。
public class ProductList
{
public string ProductID {get;set;}
public int Amount {get;set;}
}
public class MyBindingList<T>:BindingList<T> where T:ProductList
{
protected override void InsertItem(int index, T item)
{
var tempList = Items.Where(x => x.ProductID == item.ProductID);
if (tempList.Count() > 0)
{
T itemTemp = tempList.FirstOrDefault();
itemTemp.Amount += item.Amount;
}
else
{
if (index > base.Items.Count)
{
base.InsertItem(index-1, item);
}
else
base.InsertItem(index, item);
}
}
public void InsertIntoMyList(int index, T item)
{
InsertItem(index, item);
}
}
そして、このリストを使用できるクライアント コードで。
ProductList tempList = new ProductList() { Amount = 10, ProductID = "1" };
ProductList tempList1 = new ProductList() { Amount = 10, ProductID = "1" };
ProductList tempList2 = new ProductList() { Amount = 10, ProductID = "2" };
ProductList tempList3 = new ProductList() { Amount = 10, ProductID = "2" };
MyBindingList<ProductList> mylist = new MyBindingList<ProductList>();
mylist.InsertIntoMyList(0, tempList);
mylist.InsertIntoMyList(1, tempList1);
mylist.InsertIntoMyList(2, tempList2);
mylist.InsertIntoMyList(3, tempList);
mylist.InsertIntoMyList(4, tempList1);
mylist.InsertIntoMyList(0, tempList3);