C# 无法将类型“string”隐式转换为“int”数组
我得到这个错误:错误1不能隐式地将类型“string”转换为“int”C# 无法将类型“string”隐式转换为“int”数组,c#,json,C#,Json,我得到这个错误:错误1不能隐式地将类型“string”转换为“int” 我怎样才能解决这个问题?谢谢 您可以使用Linq,更改此选项: public class Product { public string name { get; set; } public string description { get; set; } public decimal price { get; set; } public int [] categories { get; s
我怎样才能解决这个问题?谢谢 您可以使用Linq,更改此选项:
public class Product
{
public string name { get; set; }
public string description { get; set; }
public decimal price { get; set; }
public int [] categories { get; set; }
}
var parentstl = from parentstyle in DBB.vParentStyles
select new
{
parentstyle.name,
parentstyle.description,
parentstyle.price,
Categories = parentstyle.categories.ToArray(),
};
foreach (var pstl in parentstl)
{
request.AddBody(new Product
{
name = pstl.name,
description = pstl.description,
price = (Decimal)pstl.price,
**categories = new int[]{pstl.categories}.ToArray()**
});
}
为此:
**categories = new int[]{pstl.categories}.ToArray()**
int.Parse或Convert.ToInt32。考虑到匿名类型中有类别,但在后面的块中使用了类别,我很惊讶它竟然会走这么远。如果您提供一个简短但完整的程序,格式正确,这会有所帮助…您的parentstyle类型是什么?parentstyle.categories元素是否可以转换为int?Selectint.Parse可以正常工作,不需要额外的lambdayep,这是肯定的。我希望写出来能给出一个清晰的解释它给了我这个错误:错误1方法“System.Linq.Enumerable.SelectSystem.Collections.Generic.IEnumerable,System.Func”的类型参数不能从用法中推断出来。尝试显式指定类型参数。我需要这样的输出。但我得到一个错误'System.Linq.Enumerable.SelectSystem.Collections.Generic.IEnum'无法从用法中推断出“System.Func”的可操作性。尝试显式指定类型参数首选输出:Value={\name\:\000TEST\,\description\:\This is Test Item\,\price\:\19.99\,\categories\:[141163],\type\:\physical}”
categories = pstl.categories.Select(int.Parse).ToArray();