本文介绍了无法转换类型WhereSelectListIterator 2 System.Collections.Generic.List的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理这些列表,以获取与组合框中的所选项目匹配的项目.

I am working on these lists to get an item that matches the selected item from the combobox.

private void InitializaMessageElement()
{
    if (_selectedTransactionWsName != null)
    {
  1. 从此处的下拉列表中获取与所选项目匹配的交易Web服务名称,输出= TestWS是正确的

  1. get a transaction webservice name matching the selected item from the drop down here the output=TestWS which is correct

var getTranTypeWsName = TransactionTypeVModel
     .GetAllTransactionTypes()
     .FirstOrDefault(transTypes => 
             transTypes.WsMethodName == _selectedTransactionWsName);

  • 从treenode列表中圈出wsnames列表.在这里,它为我提供了所有正确的节点.

  • Loop the list of wsnames from the treenode list. Here it gives me all the node I have which is correct.

    var wsNameList = MessageElementVModel
         .GetAllTreeNodes().Select(ame => 
                 ame.Children).ToList();//. == getTranTypeWsName.WsMethodName);
    

  • 在wsNameList中找到getTranTypeWsName.WsMethodName.这是我遇到的问题:

  • find the getTranTypeWsName.WsMethodName in the wsNameList. Here is where I have the problem:

          var msgElementList = wsNameList.Select(x => x.Where(ame => getTranTypeWsName != null && ame.Name == getTranTypeWsName.WsMethodName)).ToList();
    

  • 我的MsgElement列表:

    my MsgElement list:

        MsgElementObsList = new ObservableCollection<MessageElementViewModel>(msgElementList);
        this.messageElements = _msgElementList;
        NotifyPropertyChanged("MessageElements");
    }
    

    此处抛出了强制转换错误.为什么它不起作用?我是LINQ的新手.谢谢

    Here it is throwing the cast error. why is it not working? I am new to LINQ. thanks

    推荐答案

    由于错误正告诉您,LINQ方法返回特殊的迭代器类型实现IEnumerable<T>;他们不返回List<T>.
    这样可以推迟执行.

    As the error is trying to tell you, LINQ methods return special iterator types the implement IEnumerable<T>; they do not return List<T>.
    This enables deferred execution.

    由于对象实际上不是List<T>,因此无法将其强制转换为不是的类型.

    Since the object isn't actually a List<T>, you can't cast it to a type that it isn't.

    如果需要List<T>,则可以调用ToList(),也可以完全跳过LINQ并使用List<T>.ConvertAll(),这与Select()类似,但是会返回List<T>.

    If you need a List<T>, you can either call ToList(), or skip LINQ entirely and use List<T>.ConvertAll(), which is like Select(), but does return List<T>.

    这篇关于无法转换类型WhereSelectListIterator 2 System.Collections.Generic.List的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-24 03:58