仅当 BindingList 中有新的 else 更新时才插入

发布于 2024-10-03 07:27:59 字数 495 浏览 6 评论 0原文

您好,我有一个包含产品的自定义 BindingList,其中包含以下信息

string ProductID
int Amount;

我如何才能执行以下操作。

ProductsList.Add(new Product("ID1", 10));
ProductsList.Add(new Product("ID2", 5));
ProductsList.Add(new Product("ID2", 2));

然后,该列表应包含 2 个产品

ProductID = "ID1"   Amount = 10
ProductID = "ID2"   Amount = 7;

,因此它的工作方式有点像购物车,

我正在查看 AddingNew 事件并覆盖 void InsertItem(int index, T item)

但我确实需要一些帮助来入门

Hi I have a custom BindingList Containing Products with the following information

string ProductID
int Amount;

How would I make it possible to do the following.

ProductsList.Add(new Product("ID1", 10));
ProductsList.Add(new Product("ID2", 5));
ProductsList.Add(new Product("ID2", 2));

The list should then contain 2 Products

ProductID = "ID1"   Amount = 10
ProductID = "ID2"   Amount = 7;

So It workes kind of like a Shopping Cart

Im looking at the AddingNew Event and override void InsertItem(int index, T item)

But I could really need a little help getting started

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-10-10 07:27:59

我真的不知道为什么你需要这个自定义列表,因为 .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);

i really don't know why you require this custom list as there are many good collections in the .net library but i have tried something below.

 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);
    }



}

and in the client code where you can use this list.

        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);
顾挽 2024-10-10 07:27:59

创建自己的集合很少是正确的选择 - 在这种情况下我更喜欢包含而不是继承。类似评论的东西

class ProductsList
{
    private readonly SortedDictionary<string, int> _products 
                                   = new Dictionary<string,int>();

    public void AddProduct(Product product)
    {
        int currentAmount;
        _products.TryGetValue(product.ProductId, out currentAmount);

        //if the product wasn't found, currentAmount will be 0
        _products[product.ProductId] = currentAmount + product.Amount;
    }
}

  • 如果您需要一个实际的 IEnumerable (而不是字典),请使用 KeyedCollection
  • 您可以为 IEnumerable编写扩展方法,而不是创建自己的类(这会迫使您实现所需的所有方法)。 code>,类似 AddProduct - 但这不会隐藏常规的 Add 方法,我猜这更像是一种快速而肮脏的方法

最后,不要担心 IBindingList 部分 - 您始终可以使用 BindingSource

Creating your own collection is rarely the right choice - I would favor containment rather than inheritance in this case. Something like

class ProductsList
{
    private readonly SortedDictionary<string, int> _products 
                                   = new Dictionary<string,int>();

    public void AddProduct(Product product)
    {
        int currentAmount;
        _products.TryGetValue(product.ProductId, out currentAmount);

        //if the product wasn't found, currentAmount will be 0
        _products[product.ProductId] = currentAmount + product.Amount;
    }
}

Comments:

  • If you need an actual IEnumerable (as opposed to a dictionary), use a KeyedCollection
  • Instead of creating your own class (which forces you to implement all the methods you want), you can write an extension method for IEnumerable<Product>, something like AddProduct - but that won't hide the regular Add method do I guess it's more of a quick and dirty way of doing it

Lastly, don't worry about the IBindingList part - you can always use a BindingSource

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文