locked
How to selectmany for enum? RRS feed

  • Question

  • User521171331 posted

    Say for example I have this enum

    public enum Suit
    {
        Diamonds, Clubs, Hearts, Spades
    }

    How do I use LINQ selectmany to loop my enum four times?

    This is version below is not in enum but my version, the Suit is enum

    cards = new[] { "Spades", "Hearts", "Clubs", "Diamonds", }
                        .SelectMany(
                            suit => Enumerable.Range(1, 13),
                            (suit, rank) => new Card(rank, suit))
                        .ToArray();

    I tried this but it says 'Cannot implicitly convert type 'Card[]' to 'System.Collections.Generic.List<Card>'

    var suitAsList = Enum.GetValues(typeof(Suit)).Cast<Suit>().ToList();
            cards =
                    suitAsList
                        .SelectMany(
                            suit => Enumerable.Range(1, 13),
                            (suit, rank) => new Card((Suit)suit, (Face)rank))
                        .ToArray();

    My working version is below but wish to use LINQ selectmany method

            List<Card> coldDeck = new List<Card>(52);
    
            for (int j = 0; j < 4; j++)
                for (int i = 0; i < 13; i++)
                    coldDeck.Add(new Card((Suit)j, (Face)i));
            cards = coldDeck;

    Thursday, April 4, 2019 6:21 AM

Answers

  • User283571144 posted

    Hi ngaisteve1,

    According to your codes, I found you ToArray to convert the suitAsList with SelectMany method.  

    Since the suitAsList  is a list, it will generate a list after SelectMany method. This is the reason why you get the  'Cannot implicitly convert type 'Card[]' to 'System.Collections.Generic.List<Card>' error.

    If you want to get the array result, you could try to use below codes:

                var suitAsList = Enum.GetValues(typeof(Suit)).Cast<Suit>().ToArray();
    
                var cards =
      suitAsList
                .SelectMany(
                    suit => Enumerable.Range(1, 13),
                    (suit, rank) => new Card((Suit)suit, rank))
                .ToArray();

    Then it will work well.

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 5, 2019 4:39 AM

All replies

  • User283571144 posted

    Hi ngaisteve1,

    According to your codes, I found you ToArray to convert the suitAsList with SelectMany method.  

    Since the suitAsList  is a list, it will generate a list after SelectMany method. This is the reason why you get the  'Cannot implicitly convert type 'Card[]' to 'System.Collections.Generic.List<Card>' error.

    If you want to get the array result, you could try to use below codes:

                var suitAsList = Enum.GetValues(typeof(Suit)).Cast<Suit>().ToArray();
    
                var cards =
      suitAsList
                .SelectMany(
                    suit => Enumerable.Range(1, 13),
                    (suit, rank) => new Card((Suit)suit, rank))
                .ToArray();

    Then it will work well.

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 5, 2019 4:39 AM
  • User521171331 posted

    thanks. it works.

    Friday, April 5, 2019 5:21 AM