本文介绍了不能隐式转换类型'System.Collections.Generic.List< int>' '串'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
收到错误:
CS0029:不能将类型'System.Collections.Generic.List'隐式转换为'string'
不知道如何解决。
给我麻烦的部分是:
字符串qty =新列表< int>();
foreach(var item in HttpContext.Current.Request.Form.GetValues(quantity_input))
{
qty.Add(int.Parse(item));
$ / code>
完整的代码是:
CartItemCollection items = new CartItemCollection();
Cart cart = Core.GetCartObject();
string skus =;
字符串debugStr =;
产品产品= null;
string qty = new List< int>();
foreach(var item in HttpContext.Current.Request.Form.GetValues(quantity_input))
{
qty.Add(int.Parse(item));
}
try
{
string [] productNumbers = HttpContext.Current.Request.Form.GetValues(ProductNumber);
foreach(productNumbers中的字符串productNumber)
{
debugStr = debugStr +-p =+ productNumber;
if(!string.IsNullOrEmpty(productNumber.Trim())&&!productNumber.StartsWith(Enter Product#))
{
try
{//如果找不到产品,则重定向
product = Core.GetProductObjectByProductNumber(productNumber);
}
catch(Exception e)
{
debugStr = debugStr +-e = noproductfound;
继续; //什么都不做,处理下一个用户输入
}
//检查我们是否有有效的产品对象,允许虚拟和其他类型直接添加到可能需要特殊处理的购物车
if(product!= null)
{
debugStr = debugStr +-t =+ product.ProductTypeName;
if(!product.ProductTypeName.Equals(NORMAL))
{
//假设VIRTUAL(或其他类型)并重定向用于选择子/组产品或其他特殊处理
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; //在提交的下一个产品编号无效的情况下重置产品对象
} //产品不为空
} //对缺省或缺省数据进行完整性检查
} //迭代每个产品提交
cart.AddItems(items);
form.Redirect(cart.aspx?skus =+ skus);
}
catch(Exception e)
{
form.AddError(*** ProductNumber provided provided was not found ***);
form.Redirect(quickorder.aspx?qo = 2& e =+ e.Message);
return;
}
解决方案
string qty = new List< int>();
应该是
var qty = new List< int>();
Receiving the error:
CS0029: Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'
Not sure how to fix.
Section that's giving me trouble is at:
string qty = new List<int>();
foreach (var item in HttpContext.Current.Request.Form.GetValues("quantity_input"))
{
qty.Add(int.Parse(item));
}
Complete code is:
CartItemCollection items = new CartItemCollection();
Cart cart = Core.GetCartObject();
string skus = "";
string debugStr = "";
Product product = null;
string qty = new List<int>();
foreach (var 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;
}
解决方案
string qty = new List<int>();
Should be
var qty = new List<int>();
这篇关于不能隐式转换类型'System.Collections.Generic.List< int>' '串'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!