【问题标题】:C#, No overload for method 'ToList' takes 1 argumentsC#,方法“ToList”没有重载需要 1 个参数
【发布时间】:2013-05-05 10:01:57
【问题描述】:

我尝试将 \n 拆分为元素,将每个元素拆分为 , 作为节点属性,然后填充一个 treeNode 列表

        List<TreeNode> views = new List<TreeNode>();

        views = res.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)   // first split, dump garbage :)
                .Select(line => line.Split(','))   // second split, each line 
                .ToList(t => new TreeNode
                {
                    Text = t[0],
                    ToolTipText = t[1]
                }

                );

看起来像ToList() failed,我以前用来填充字典、列表等。 错误是

方法 'ToList' 没有重载需要 1 个参数

有什么提示吗?

【问题讨论】:

  • Select(....).ToList()(这可能也是一个不错的扩展方法)

标签: c#


【解决方案1】:

你需要一个额外的 Select :

 List<TreeNode> views =  res  // new List<TreeNode>();    
      .Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)   
      .Select(line => line.Split(','))   // second split, process each line 
      .Select(t => new TreeNode
      {
        Text = t[0],
        ToolTipText = t[1]
      })
      .ToList( );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多