【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<int>' to 'int'无法将类型“System.Collections.Generic.List<int>”隐式转换为“int”
【发布时间】:2014-04-03 00:05:56
【问题描述】:

收到错误:

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

不知道如何解决。

我正在使用:

Microsoft .NET 框架版本: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);
}

具体来说,item.Quantity = qty;部分

完整代码为:

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

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

【问题讨论】:

  • 这看起来像是this question 的后续问题,但来自两个不同的用户。
  • 错误很明显,错误信息告诉你到底哪里出了问题。我们无法猜测您的意图。你想用那条线实现什么?

标签: c# asp.net generics


【解决方案1】:

你的问题出在这一行:

 item.Quantity = qty;  

item.Quantity 是一个 int 并且 qty 是一个 List&lt;int&gt;


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

int index = 0; // add this line
foreach (string productNumber in productNumbers)
{

 // all the stuff you have already till:

                        item.Quantity = qty[index];

 // all the stuff you have already

   index = index + 1;
}  //iterate on each product submitted

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

一个好的解决方案是创建一个数据结构,将产品编号和数量保存在同一个列表中。

【讨论】:

  • 好的,很酷。我怎样才能让List&lt;int&gt;进入item.Quantity的int?
  • @user3490200 - 我会猜测并发布编辑。
  • 好吧,没有更多错误,但我想要的结果不起作用。本质上,我希望通过快速订购表将产品添加到购物车。 rsatestamle.dminsite.com/quickorder.aspx
  • @user3490200:qty 必须是 int 而不是 List&lt;int&gt; OR item.Quantity 必须是 List&lt;int&gt; OR item.Quantity = qty.Sum(); 从列表中选择一项item.Quantity = qty[someIndex];
  • @OlivierJacot-Descombes 是的,当我继承这段代码时,item.Quantity = 1; 是硬编码的,仅此而已。所以我正在尝试添加输入数字和快速订购更多商品的功能。
猜你喜欢
  • 1970-01-01
  • 2015-01-15
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
相关资源
最近更新 更多