简体   繁体   English

如何仅将项目添加到以前未添加的列表中? (C# - ASP.NET)

[英]How to only add items to a list that haven't been added before? (C# - ASP.NET)

I have a list that is being populated with the summed quantities of specific product ID's, for example:我有一个列表,其中包含特定产品 ID 的总数量,例如:

My table:我的表:

在此处输入图片说明

My program checks for a user input and then compares that user input to the Quantity column in the table - once a match is found, the program checks for the ProductID of the matched quantity, and then adds ALL the quantities relating to that matched ProductID.我的程序检查用户输入,然后将该用户输入与表中的 Quantity 列进行比较 - 一旦找到匹配项,程序将检查匹配数量的 ProductID,然后添加与该匹配 ProductID 相关的所有数量。

For example, if a user enters 2, the program will check for all the values in the Quantity column greater than or equal to 2 - so in this case, the first row to be found would be row 2 (which has a productID of 9 and a quantity of 2).例如,如果用户输入 2,程序将检查 Quantity 列中大于或等于 2 的所有值 - 因此在这种情况下,要找到的第一行将是第 2 行(其中 productID 为 9数量为 2)。 The program should now search for ALL productID's equal to 9 and then sum their individual quantities (which will be 3 in this case, as the ProductID 9 appears only twice, with the quantities 2 and 1 respectively), and then save that final summed quantity (3) into the list.程序现在应该搜索所有等于 9 的 productID,然后对它们的各个数量求和(在这种情况下为 3,因为 ProductID 9 只出现两次,数量分别为 2 和 1),然后保存最终的总数量(3)进入列表。 And then the program should continue to row 3 (which has a productID of 8 and a quantity of 3).然后程序应该继续到第 3 行(productID 为 8,数量为 3)。 The program should now search for ALL productID's equal to 8 and then sum their individual quantities (which will be 9 in this case, as the ProductID 8 appears three times, with the quantities 3 and 3 and 3 respectively), and then save that final summed quantity (9) into the list.程序现在应该搜索所有等于 8 的产品 ID,然后将它们各自的数量相加(在本例中为 9,因为产品 ID 8 出现了 3 次,数量分别为 3、3 和 3),然后保存最终的将数量 (9) 加到列表中。 And then the program should SKIP row 4, because the ProductID of 9 has already been dealt with - and so on and so forth.然后程序应该跳过第 4 行,因为 9 的 ProductID 已经被处理 - 依此类推。

So far i have tried this:到目前为止,我已经尝试过这个:

var dynamicReader = DBAccessor.InvoiceLines.Where(xx => xx.Quantity >= quantitySelected).Select(yy => yy.ProductID);

            foreach (var product in dynamicReader)
            {
                if (!quantityArrayList.Contains(product))
                {
                    quantityArrayList.Add(DBAccessor.InvoiceLines.Where(gg => gg.ProductID == product).Sum(g => g.Quantity));
                }
            }

If the user enters 2, the first 4 results using this method are correct, however, after that, the summed values fetched make little sense to me.如果用户输入 2,则使用此方法的前 4 个结果是正确的,但是,在此之后,获取的总和值对我来说意义不大。 Any help on this matter would be greatly appreciated.对此事的任何帮助将不胜感激。

It looks like you're not associating the ProductID with the Quantity stored in quantityArrayList .看起来您没有将ProductID与存储在quantityArrayListQuantity相关联。 So, when you check quantityArrayList.Contains(product) you're not checking to see if that ProductID has already been processed, you're checking to see if there is a sum of quantities which match the ProductID .因此,当您检查quantityArrayList.Contains(product)您不是在检查该ProductID是否已被处理,而是检查是否存在与ProductID匹配的数量总和。

I don't know what type quantityArrayList is, I'm assuming it's a List<int> .我不知道quantityArrayList是什么类型,我假设它是一个List<int> Instead, you should use a Dictionary where the key would be the ProductID and the value would be the sum of the Quantity .相反,您应该使用Dictionary ,其中键是ProductID ,值是Quantity的总和。

Untested, but the updated code might look like...未经测试,但更新后的代码可能看起来像......

Dictionary<int, int> productQuantities = new Dictionary<int, int>(); //I've changed the name, but this would replace quantityArrayList

var dynamicReader = DBAccessor.InvoiceLines.Where(xx => xx.Quantity >= quantitySelected).Select(yy => yy.ProductID);

foreach (var product in dynamicReader)
{
    if (!productQuantities.ContainsKey(product))
    {
        productQuantities.Add(product, DBAccessor.InvoiceLines.Where(gg => gg.ProductID == product).Sum(g => g.Quantity));
    }
}

I would use GroupBy with 2 values mapped to the grouping: Max and Sum .我会将GroupBy与映射到分组的 2 个值一起使用: MaxSum If the max quantity of one of the entries is greater than your desired amount, that grouping is 'in scope' and you want to get its sum.如果其中一个条目的最大数量大于您想要的数量,则该分组在“范围内”并且您想要获得其总和。 Like this:像这样:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Program
    {
        public static void Main()
        {
            var productCounts = new List<ProductCount>{
                new ProductCount{ProductId = 10, Quantity = 1},
                new ProductCount{ProductId = 9, Quantity = 2},
                new ProductCount{ProductId = 8, Quantity = 3},
                new ProductCount{ProductId = 9, Quantity = 1},
                new ProductCount{ProductId = 7, Quantity = 2},
                new ProductCount{ProductId = 5, Quantity = 3},
                new ProductCount{ProductId = 8, Quantity = 3},
                new ProductCount{ProductId = 6, Quantity = 2}
            };
            
            var desiredMinimum = 3;
            
            var output = productCounts.GroupBy(i => i.ProductId)
                .Select(i => new
                        {
                            ProductId = i.Key,
                            Max = i.Max(j => j.Quantity),
                            Total = i.Sum(j => j.Quantity)
                        })
                .Where(i => i.Max >= desiredMinimum)
                .ToList();
            
            foreach(var outputItem in output){
                Console.WriteLine("ProductId: " + outputItem.ProductId + "; Total: " + outputItem.Total);
            }
        }
        internal class ProductCount
        {
            public int ProductId{get;set;}
            public int Quantity{get;set;}
        }
    }

See:看:

https://dotnetfiddle.net/HmMc4h https://dotnetfiddle.net/HmMc4h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM