我正在试验 jstree,我正在使用一个 mvc 项目来填充树。
到目前为止它运行良好,但现在我决定将一个属性从字符串更改为 int。
我这样做是因为我正在更改的属性是一个 ID 属性,我想从我拥有的列表中获取最高的 ID 并将其递增 1。
代码:
List<TreeNode> Nodes = getTreenodeList();
var NewId = Nodes.Select(x => x.Id.Max()) +1;
上面的代码给我以下错误: “无法从‘int’转换为‘System.Collections.Generic.IEnumerable’
获取树节点列表:
public static List<TreeNode> getTreenodeList()
{
var treeNodes = new List<TreeNode>
{
new TreeNode
{
Id = 1,
Text = "Root"
},
new TreeNode
{
Id = 2,
Parent = "Root",
Text = "Child1"
}
,
new TreeNode
{
Id = 3,
Parent = "Root",
Text = "Child2"
}
,
new TreeNode
{
Id = 4,
Parent = "Root",
Text = "Child3"
}
};
// call db and get all nodes.
return treeNodes;
}
最后是 treeNode 类:
public class TreeNode
{
[JsonProperty(PropertyName = "id")]
public int Id { get; set; }
[JsonProperty(PropertyName = "parent")]
public string Parent { get; set; }
[JsonProperty(PropertyName = "text")]
public string Text { get; set; }
[JsonProperty(PropertyName = "icon")]
public string Icon { get; set; }
[JsonProperty(PropertyName = "state")]
public TreeNodeState State { get; set; }
[JsonProperty(PropertyName = "li_attr")]
public string LiAttr { get; set; }
[JsonProperty(PropertyName = "a_attr")]
public string AAttr { get; set; }
}
到目前为止,我的谷歌搜索结果通过使用 firstorDeafut 进行了几次尝试,我发现它应该将 ienumrable 转换为 int,但遗憾的是这没有用。我尝试了其他几种情况,但都没有帮助。
老实说,我真的不明白这里的问题是什么,所以如果有人有答案,我也将非常感谢您的解释。
谢谢!
最佳答案
此语句(如果有效)
Nodes.Select(x => x.Id.Max())
会返回 IEnumerable<int>
而不是一个Int
.将其替换为:
Nodes.Select(x => x.Id).Max()
还有你的领域Id
将持有一个 Value
, 所以要申请 Max
就错了。
您的代码应该是:
var NewId = Nodes.Select(x => x.Id).Max() + 1;
关于c# - 如何解决以下错误 : cannot convert from 'int' to 'System. Collections.Generic.IEnumerable<int>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268052/