TreeView:无法转换为&#39; System.Collections.Generic.IEnumerable <string>&#39;到&#39; System.Collections.Generic.IEnumerable </string>

时间:2013-06-08 14:47:09

标签: c# wpf mvvm treeview

我正在研究WPF项目,以显示TreeView中的路径列表。我有propertyPaths(例如:NetworkControl.AlternateIndexText.Value“)路径有id。

的TreeModel

public class MessageElement
{
    private int id;
     public string Name { get; set; }
    public string path { get; set; }
    public List<MessageElement> Children { get; set; }
    public List<MessageElement> messageElements { get; set; }

   public MessageElement()
    {
        Children = new List<MessageElement>();
        messageElements = new List<MessageElement>();
    }

    public MessageElement(int id, string name, List<MessageElement> children)
    {
        this.ID = id;
        this.Name = name;
        this.Children = children;

    }


    public MessageElement(int id, string path)
    {

        this.ID = id;
        this.path = path;

    }
}

moq一些数据

   public List<MessageElement> GetRequestTreeNodes()
    {

        messageElements.Add(new MessageElement(1, "NetworkControl.AlternateIndexText.Value"));
        messageElements.Add(new MessageElement(2, "NetworkControl.AddressData.DestinationID"));
        messageElements.Add(new MessageElement(2, "NetworkControl.AddressData.MessageOriginatorID.Value"));


        messageElements.Add(new MessageElement(3, "VehicleSummary.VehicleIdentification.IdentificationID.Value"));
        messageElements.Add(new MessageElement(3, "TitleSummary.JurisdictionTitlingKeyText.Value"));
        messageElements.Add(new MessageElement(1, "VehicleSummary.VehicleIdentification.IdentificationID.Value"));



        return messageElements;

}

递归创建树:

   public List<MessageElement> BuildTree(IEnumerable<MessageElement> messageElements)
    {

        return (
          from element in messageElements           // Ex:(1, "NetworkControl.AlternateIndexText.Value")
          let elementId = element.id                       // get id from message element
          let splitPath = element.path.Split('.')   // get path from  message element
          group element by element.path.Split('.')[0] into pathElementGroup

          select new MessageElement(ID, path)
          {
             ID = elementId,  ??                                 // id of each path  Ex: 1 => "NetworkControl.AlternateIndexText.Value"
              Name = pathElementGroup.Key,             //name of each tree node to be displayed on tree
              Children = BuildTree(                    //create child from the propertyPath
                 (from propertyPathElement in pathElementGroup
                  where propertyPathElement.path.Length > pathElementGroup.Key.Length + 1
                  select new MessageElement())
                 .ToList<MessageElement>())
          }
          );

    }


    }

如何删除此异常以便我可以递归地构建我的树?

2 个答案:

答案 0 :(得分:0)

因为 LINQ 查询会返回一些 IEnumerable&lt; T&gt; ,而不是列表&lt; T&gt; ,所以您必须应用 ToList

    BuildTree(                    //create child from the propertyPath
    (from propertyPathElement in pathElementGroup
    where propertyPathElement.path.Length > pathElementGroup.Key.Length + 1
    select propertyPathElement.path.Substring(pathElementGroup.Key.Length + 1)).ToList())
          }

答案 1 :(得分:0)

尝试使用此代码,您需要在将数据投影到Build List函数时创建MessageElement,该函数接受List<MessageElement >

另外,为什么不接受IEnumerable<MessageElement>作为BuildList方法的输入参数?

var result = BuildTree(0, GetRequestTreeNodes());

 public List<MessageElement> BuildTree(int depth, List<MessageElement> messageElements)
        {
            if (messageElements == null)
            {
                return null;
            }
            return (
                  from element in BuildGrouping(depth, messageElements)           // Ex:(1, "NetworkControl.AlternateIndexText.Value")
                  let elementId = element.Key                      // get id from message element

                  select new MessageElement()
                  {
                      ID = depth,                                  // id of each path  Ex: 1 => "NetworkControl.AlternateIndexText.Value"
                      Name = element.Key,             //name of each tree node to be displayed on tree
                      Children = BuildTree(depth +1,                    //create child from the propertyPath
                        element.ToList<MessageElement>())
                  }
                  ).ToList<MessageElement>();

        }

        public IEnumerable<IGrouping<string, MessageElement>> BuildGrouping(int depth, List<MessageElement> messageElements)
        {
            string key = string.Empty;
            if (messageElements != null && messageElements.Count > 0)
            {
                string[] splits = messageElements[0].path.Split('.');
                if (splits.Length > depth)
                {
                    key = splits[depth];
                }
            }

            if (string.IsNullOrEmpty(key))
            {
                return new List<IGrouping<string, MessageElement>>();
            }

            return from element in messageElements group element by element.path.Split('.')[depth];
        }