Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<QuickTest.Stock>'

mstagg

I have the following code:

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 is an object that has string properties TickerSymbol and Name. It also has double properties Price, ChangeDollars, and ChangePercent.

The second LINQ statement I wrote is throwing an error with the message, "Cannot implicitly convert type 'int' to 'System.Collections.Generic.List'". I am confused as to where the type 'int' is coming from and how to fix this error, as I dont use any int values anywhere in the program.

I am very new to LINQ as well, this is my first time using it. This error could very well be a result of some intricacy of LINQ that I am not aware of.

Anyone know why this error is happening and how to fix it?

Christos

It is reasonable the error you get, because RemoveAll returns the number of removed stocks. This is an integer. Then you try to assign this to the variable called ticker, which holds a list of objects of type Stock.

That you probably want is to remove all the Stocks that their TickerSymbol is gm and then write the Stocks that they have been left in ticker to the console. In order to do so, you could just try this:

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

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

Furthermore, for the record, as it is stated in MSDN:

The method List<T>.RemoveAll()

Removes all the elements that match the conditions defined by the specified predicate.

It's signature is the following:

public int RemoveAll(Predicate<T> match)

The Predicate<T> is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the Predicate delegate, and the elements that match the conditions are removed from the List.

This method performs a linear search; therefore, this method is an O(n) operation, where n is List's Count property.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot implicitly convert type to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

Cannot implicitly convert anonymous type to System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'int?' to 'System.Collections.Generic.List<Model>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

From Dev

Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

From Dev

C#: Error-Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<double>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

From Dev

Cannot Implicitly Convert Type 'System.Collections.Generic.List<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

From Dev

Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type System.Collections.Generic.List<IEnumerable> to <IEnumerable

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C#

From Dev

C# Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.ICollection

Related Related

  1. 1

    Cannot implicitly convert type to 'System.Collections.Generic.List

  2. 2

    Cannot implicitly convert type 'System.Collections.Generic.List

  3. 3

    Cannot implicitly convert type System.Collections.Generic.List

  4. 4

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  5. 5

    Cannot implicitly convert anonymous type to System.Collections.Generic.List

  6. 6

    Cannot implicitly convert type 'int?' to 'System.Collections.Generic.List<Model>

  7. 7

    Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

  8. 8

    Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

  9. 9

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  10. 10

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  11. 11

    Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

  12. 12

    Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

  13. 13

    Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

  14. 14

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

  15. 15

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

  16. 16

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room

  17. 17

    Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

  18. 18

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

  19. 19

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

  20. 20

    Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

  21. 21

    C#: Error-Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<double>'

  22. 22

    Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

  23. 23

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

  24. 24

    Cannot Implicitly Convert Type 'System.Collections.Generic.List<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

  25. 25

    Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

  26. 26

    Cannot implicitly convert type System.Collections.Generic.List<IEnumerable> to <IEnumerable

  27. 27

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C#

  28. 28

    C# Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'

  29. 29

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.ICollection

HotTag

Archive