简体   繁体   English

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

[英]TreeView: Cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable

I am working on WPF project to display a list of path in a TreeView . 我正在WPF项目上在TreeView显示路径列表。 I have propertyPaths (Ex: NetworkControl.AlternateIndexText.Value") paths have ids. 我有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 some data 收集一些数据

   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;

} }

recursively create tree: 递归创建树:

   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>())
          }
          );

    }


    }

How can I remove this exception so that i can build my tree recursively ? 如何删除此异常,以便可以递归地构建树?

Because a LINQ query returns some IEnumerable<T> , not a List<T> , so you have to apply ToList : 因为LINQ查询返回一些IEnumerable <T>而不是List <T> ,所以您必须应用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())
          }

Try to use this code, you need to create MessageElement as you are projecting your data to Build List function which accepts List<MessageElement > 尝试使用此代码,在将数据投影到接受List<MessageElement > Build List函数时,需要创建MessageElement。

On a side note, why are you not accepting IEnumerable<MessageElement> as input parameter to BuildList method? 附带说明一下,为什么不接受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];
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从&#39;System.Collections.Generic.IEnumerable转换为System.Collections.Generic.IEnumerable <string> - Cannot convert from 'System.Collections.Generic.IEnumerable to System.Collections.Generic.IEnumerable<string> 无法从“ System.Collections.Generic.IEnumerable”转换为System.Collections.Generic.IEnumerable <string> - cannot convert from 'System.Collections.Generic.IEnumerable' to System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换 <char> &#39;到&#39;字符串&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string' 无法从“System.Collections.Generic.IEnumerable”转换<string> &#39; 到 &#39;T&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'T' 无法从&#39;System.Collections.Generic.IEnumerable转换 <int> &#39;到&#39;System.Collections.Generic.IEnumerable <int?> “ - Cannot convert from 'System.Collections.Generic.IEnumerable<int>' to 'System.Collections.Generic.IEnumerable<int?>' 从&#39;string&#39;转换为&#39;System.Collections.Generic.IEnumerable <string> - Convert from 'string' to 'System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换 <System.Collection.Generic.List<char> &gt;字符串[] - Cannot convert from 'System.Collections.Generic.IEnumerable<System.Collection.Generic.List<char>> to string[] 无法隐式转换类型&#39;System.Collections.Generic.IEnumerable <string> &#39;到&#39;System.Collections.Generic.ICollection <x> “ - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.ICollection<x>' 无法从 System.Collections.Generic.List 转换为 System.Collections.Generic.IEnumerable - cannot convert from System.Collections.Generic.List to System.Collections.Generic.IEnumerable 转换&#39;System.Collections.Generic.IEnumerable <string> &#39;至&#39;string [] - Convert 'System.Collections.Generic.IEnumerable<string>' to 'string[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM