无法将类型'int'隐式转换为'System.Collections.Generic.List <QuickTest.Stock>'

斯塔格

我有以下代码:

static void Main(string[] args)
    { 
        List<Stock> ticker = new List<Stock>();
        ticker.Add(new Stock("msft"));
        ticker.Add(new Stock("acw"));
        ticker.Add(new Stock("gm"));

        ticker = ticker.OrderBy(s => s.Name).ToList();

        foreach (Stock s in ticker)
        {
            Console.WriteLine(s.Name);
        }

        Console.WriteLine("\n");
        ticker = ticker.RemoveAll(s => s.TickerSymbol == "gm");

        foreach (Stock s in ticker)
        {
            Console.WriteLine(s.Name);
        }
    }

Stock是具有字符串属性TickerSymbol和Name的对象。它还具有价格,ChangeDollars和ChangePercent的双重属性。

我写的第二条LINQ语句引发错误消息,“无法将类型'int'隐式转换为'System.Collections.Generic.List'”。我对类型'int'的来源以及如何解决此错误感到困惑,因为我不在程序中的任何地方使用任何int值。

我也是LINQ的新手,这是我第一次使用它。这个错误很可能是由于我不知道的LINQ的复杂性造成的。

任何人都知道为什么会发生此错误,以及如何解决该错误?

克里斯托斯

您得到的错误是合理的,因为RemoveAll返回已删除库存的数量。这是一个整数。然后,您尝试将其分配给名为的变量ticker,该变量包含类型的对象列表Stock

你可能想要的是去除所有的股票,他们TickerSymbolgm然后写股票,他们一直留在股票控制台。为此,您可以尝试以下操作:

// This will remove all the stocks you want.
ticker.RemoveAll(s => s.TickerSymbol == "gm");

foreach (Stock s in ticker)
{
    Console.WriteLine(s.Name);
}

此外,作为记录,如MSDN中所述

方法 List<T>.RemoveAll()

删除所有与指定谓词定义的条件匹配的元素。

它的签名如下:

public int RemoveAll(Predicate<T> match)

Predicate<T>是代表对,如果传递给它的对象匹配在委托中定义的条件,则返回true的方法。当前List的元素将分别传递给Predicate委托,并且符合条件的元素将从List中删除

该方法执行线性搜索。因此,此方法是O(n)运算,其中n是List的Count属性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

错误“无法将类型“System.Collections.Generic.IEnumerable<int>”隐式转换为“System.Collections.Generic.List<int>”

来自分类Dev

无法将类型“字符串”隐式转换为“System.Collections.Generic.List<int>”

来自分类Dev

无法将类型'System.Collections.IList'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型“System.Collections.Generic.List<<匿名类型:>>”隐式转换为“System.Collections.Generic.List”

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List'

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List <>

来自分类Dev

无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List<Model.Room”

来自分类Dev

无法将类型隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型'void'隐式转换为'System.Collections.Generic.List <string>'

来自分类Dev

无法将类型System.Collections.Generic.List <IEnumerable>隐式转换为<IEnumerable

来自分类Dev

无法将类型“ void”隐式转换为“ System.Collections.Generic.List”以用于返回语句

来自分类Dev

无法将类型'System.Collections.Generic.List'隐式转换为'string'

来自分类Dev

无法将匿名类型隐式转换为 System.Collections.Generic.List

来自分类Dev

无法将类型“System.Collections.Generic.List<Object>”隐式转换为“string”

来自分类Dev

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

来自分类Dev

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

来自分类Dev

无法隐式转换类型'int?' 到'System.Collections.Generic.List <Model>

来自分类Dev

无法隐式转换类型'System.Collections.Generic.List

来自分类Dev

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Threading.Tasks.Task <>>

来自分类Dev

C#:错误-无法将类型'int'隐式转换为'System.Collections.Generic.IEnumerable <double>'

来自分类Dev

无法将类型'System.Collections.Generic.List <System.Data.DataRow>'隐式转换为'System.Collections.Generic.List <string>'

来自分类Dev

无法将类型'System.Collections.Generic.List <MODEL#1>'隐式转换为'System.Collections.Generic.List <Model#2>

来自分类Dev

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <string>'

来自分类Dev

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <FirstApp.Model.TeamDetails>

来自分类Dev

无法将类型'System.Collections.Generic.List <String>'隐式转换为'System.Collections.Generic.IEnumerable <turon.Model.Products_Products>

来自分类Dev

无法将类型'System.Collections.Generic.List <string>'隐式转换为'System.Collections.Generic.IList <LM_WebApi.Controllers.Items>

来自分类Dev

无法将类型'System.Collections.Generic.Lis <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <AdventureCycle.Models.Product>'

Related 相关文章

  1. 1

    错误“无法将类型“System.Collections.Generic.IEnumerable<int>”隐式转换为“System.Collections.Generic.List<int>”

  2. 2

    无法将类型“字符串”隐式转换为“System.Collections.Generic.List<int>”

  3. 3

    无法将类型'System.Collections.IList'隐式转换为'System.Collections.Generic.List

  4. 4

    无法将类型“System.Collections.Generic.List<<匿名类型:>>”隐式转换为“System.Collections.Generic.List”

  5. 5

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List'

  6. 6

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List

  7. 7

    无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List

  8. 8

    无法将类型System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List <>

  9. 9

    无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List<Model.Room”

  10. 10

    无法将类型隐式转换为'System.Collections.Generic.List

  11. 11

    无法将类型'void'隐式转换为'System.Collections.Generic.List <string>'

  12. 12

    无法将类型System.Collections.Generic.List <IEnumerable>隐式转换为<IEnumerable

  13. 13

    无法将类型“ void”隐式转换为“ System.Collections.Generic.List”以用于返回语句

  14. 14

    无法将类型'System.Collections.Generic.List'隐式转换为'string'

  15. 15

    无法将匿名类型隐式转换为 System.Collections.Generic.List

  16. 16

    无法将类型“System.Collections.Generic.List<Object>”隐式转换为“string”

  17. 17

    无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

  18. 18

    无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

  19. 19

    无法隐式转换类型'int?' 到'System.Collections.Generic.List <Model>

  20. 20

    无法隐式转换类型'System.Collections.Generic.List

  21. 21

    无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Threading.Tasks.Task <>>

  22. 22

    C#:错误-无法将类型'int'隐式转换为'System.Collections.Generic.IEnumerable <double>'

  23. 23

    无法将类型'System.Collections.Generic.List <System.Data.DataRow>'隐式转换为'System.Collections.Generic.List <string>'

  24. 24

    无法将类型'System.Collections.Generic.List <MODEL#1>'隐式转换为'System.Collections.Generic.List <Model#2>

  25. 25

    无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <string>'

  26. 26

    无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <FirstApp.Model.TeamDetails>

  27. 27

    无法将类型'System.Collections.Generic.List <String>'隐式转换为'System.Collections.Generic.IEnumerable <turon.Model.Products_Products>

  28. 28

    无法将类型'System.Collections.Generic.List <string>'隐式转换为'System.Collections.Generic.IList <LM_WebApi.Controllers.Items>

  29. 29

    无法将类型'System.Collections.Generic.Lis <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <AdventureCycle.Models.Product>'

热门标签

归档