C# 无法隐式转换类型';System.Collections.Generic.List<;int>';至';int';

C# 无法隐式转换类型';System.Collections.Generic.List<;int>';至';int';,c#,asp.net,generics,C#,Asp.net,Generics,接收错误: CS0029:无法将类型“System.Collections.Generic.List”隐式转换为“int” 不知道如何修复 我正在使用: Microsoft.NET Framework版本:2.0.50727.5477 ASP.NET版本:2.0.50727.5479 给我带来麻烦的部分是: { debugStr = debugStr + "-a=noattributesadd"; CartItem item = new CartItem(context);

接收错误:

CS0029:无法将类型“System.Collections.Generic.List”隐式转换为“int”

不知道如何修复

我正在使用:

Microsoft.NET Framework版本:2.0.50727.5477

ASP.NET版本:2.0.50727.5479

给我带来麻烦的部分是:

{
    debugStr = debugStr + "-a=noattributesadd";
    CartItem item = new CartItem(context);
    item.ProductId = product.ProductId;
    item.Quantity = qty;  
    items.Add(item);
}
具体来说,项目。数量=数量;部分

完整代码为:

CartItemCollection items = new CartItemCollection();
Cart cart = Core.GetCartObject();
string skus = "";
string debugStr = "";
Product product = null;
List<int> qty = new List<int>();
foreach (string item in HttpContext.Current.Request.Form.GetValues("quantity_input"))

{
    qty.Add(int.Parse(item));
}

try
{
    string[] productNumbers = HttpContext.Current.Request.Form.GetValues("ProductNumber");
    foreach (string productNumber in productNumbers)
    {
        debugStr = debugStr + "-p=" + productNumber;
        if(!string.IsNullOrEmpty(productNumber.Trim()) && !productNumber.StartsWith("Enter Product #"))
            {
                try
                {   //redirect if no product found 
                    product = Core.GetProductObjectByProductNumber(productNumber);
                }
                catch (Exception e)
                {
                    debugStr = debugStr + "-e=noproductfound";
                    continue; //do nothing, process the next user input
                }
                //check if we have a valid product object, allow virtual and other type(s) for adding directly to cart which may need special handling
                if(product != null)
                    {
                        debugStr = debugStr + "-t=" + product.ProductTypeName;
                        if(!product.ProductTypeName.Equals("NORMAL"))
                        {
                            //assume VIRTUAL (or other type) and redirect for selecting child/group products or other special handling
                            form.Redirect("product.aspx?p=" + product.ProductNumber);
                        }
                        else
                        {
                            debugStr = debugStr + "-a=noattributesadd";
                            CartItem item = new CartItem(context);
                            item.ProductId = product.ProductId;
                            item.Quantity = qty;  
                            items.Add(item);
                        }
                        skus = skus + ";" + productNumber;
                        product = null;  //reset the product object in case the next product number submitted is invalid
                    }  //product not null
            }  //sanity check for empty or default data
    }  //iterate on each product submitted
cart.AddItems(items);
form.Redirect("cart.aspx?skus=" + skus);
}
catch (Exception e)
{
    form.AddError("*** ProductNumber provided was not found ***");
    form.Redirect("quickorder.aspx?qo=2&e=" + e.Message);
    return;
}
CartItemCollection items=新的CartItemCollection();
购物车=Core.GetCartObject();
字符串SKU=“”;
字符串debugStr=“”;
Product=null;
列表数量=新列表();
foreach(HttpContext.Current.Request.Form.GetValues(“数量\输入”)中的字符串项)
{
数量添加(内部分析(项目));
}
尝试
{
string[]ProductNumber=HttpContext.Current.Request.Form.GetValues(“ProductNumber”);
foreach(productNumber中的字符串productNumber)
{
debugStr=debugStr+“-p=“+productNumber;
如果(!string.IsNullOrEmpty(productNumber.Trim())&&!productNumber.StartsWith(“输入产品”)
{
尝试
{//如果找不到产品,则重定向
product=Core.GetProductObjectByProductNumber(productNumber);
}
捕获(例外e)
{
debugStr=debugStr+“-e=noproductfound”;
continue;//不执行任何操作,处理下一个用户输入
}
//检查我们是否有有效的产品对象,允许虚拟和其他类型的产品直接添加到购物车,可能需要特殊处理
如果(产品!=null)
{
debugStr=debugStr+“-t=“+product.ProductTypeName;
如果(!product.ProductTypeName.Equals(“NORMAL”))
{
//假设虚拟(或其他类型)并重定向以选择子/组产品或其他特殊处理
表单重定向(“product.aspx?p=“+product.ProductNumber”);
}
其他的
{
debugStr=debugStr+“-a=noattributesadd”;
CartItem=新的CartItem(上下文);
item.ProductId=product.ProductId;
项目数量=数量;
项目。添加(项目);
}
SKU=SKU+“;”+产品编号;
product=null;//如果提交的下一个产品编号无效,请重置产品对象
}//产品不为空
}//检查空数据或默认数据的完整性
}//迭代提交的每个产品
购物车附加项(项目);
表单重定向(“cart.aspx?skus=“+skus”);
}
捕获(例外e)
{
表.补遗(“***未找到提供的产品编号***”);
表单重定向(“quickorder.aspx?qo=2&e=“+e.Message”);
返回;
}

本质上,这是快速订单的逻辑。我正在尝试将每个项目的数量添加到购物车中

您的问题在这一行:

 item.Quantity = qty;  
项目。数量
是整数,数量是列表


猜测如何求解(假设所有列表的顺序相同,枚举将以相同的顺序读取):

注意:我讨厌这个解决方案。但它可能会起作用


一个好的解决方案是创建一个数据结构,将productnumber和quantity保存在同一个列表中。

您的问题在这一行:

 item.Quantity = qty;  
项目。数量
是整数,数量是列表


猜测如何求解(假设所有列表的顺序相同,枚举将以相同的顺序读取):

注意:我讨厌这个解决方案。但它可能会起作用


一个好的解决方案是创建一个数据结构,将productnumber和quantity都保存在同一个列表中。

这看起来像是来自两个不同用户的后续问题。错误很明显,错误消息会准确地告诉您出了什么错。我们猜不出你打算做什么。你想用这行代码实现什么?这看起来像是两个不同用户的后续问题。错误是显而易见的,错误消息会准确地告诉你出了什么问题。我们猜不出你打算做什么。你想用这条线达到什么目的?好的,酷。如何使
列表
进入
项目的整数。数量
?@user3490200-我将进行猜测并发布编辑。好吧,没有更多错误,但我想要的结果不起作用。基本上,我希望通过快速订单将产品添加到购物车中@用户3490200:
quaty
必须是
int
而不是
List
项。Quantity
必须是
List
项。Quantity=Quantity.Sum()或从列表中选择一项
item.Quantity=qty[someIndex]@OlivierJacot Descombes是的,当我继承这个代码时,它是硬编码的
item.Quantity=1就是这样。所以我尝试添加输入数字和快速订购更多物品的功能。好的,酷。如何使
列表
进入
项目的整数。数量
?@user3490200-我将进行猜测并发布编辑。好吧,没有更多错误,但我想要的结果不起作用。基本上,我希望通过快速订单将产品添加到购物车中@用户3490200:
quaty
必须是
int
而不是
List
项。Quantity
必须是
List
项。Quantity=Quantity.Sum()或从列表中选择一项
item.Quantity=qty[someIndex]@OlivierJacot Descombes是的,当我继承这个代码时,它是硬编码的
item.Quantity=1就是这样。所以我在努力