Laserbeak43
Laserbeak43

Reputation: 609

C#/LINQ Querying a class

I'm using Sam Jenkin's Deck of Cards class. I'm trying to query certain cards in a suit. I'm getting the error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

I've tried moving things around but I'm not understanding the error. Could someone please help? My code is:

var deck = new Deck();

IEnumerable<Deck> deckQuery =
            from myCard in deck.Cards
            where myCard.Suit == Suit.Club
            select myCard.CardNumber;

My Card class is:

public class Card : ICard
{
    public Suit Suit { get; set; }
    public CardNumber CardNumber { get; set; }
}

My enumerators are:

public enum Suit
{
    Club = 1,
    Diamond = 2,
    Heart = 3,
    Spades = 4,
}

public enum CardNumber
{
    Ace = 1,
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
    Seven = 7,
    Eight = 8,
    Nine = 9,
    Ten = 10,
    Jack = 11,
    Queen = 12,
    King = 13,
}

Upvotes: 1

Views: 112

Answers (3)

Hector Sanchez
Hector Sanchez

Reputation: 2317

You are saying

select myCard.CardNumber;

According to your definition

public CardNumber CardNumber { get; set; }

It returns a CardNumber

And you are expecting a IEnumerable<Deck> instead of IEnumerable<CardNumber>

If you are trying to get the cards, then just return that

select myCard

And it Should be IEnumerable<Card>

The Deck class is the one that helps you to do some operations with your cards. I think you are looking for returning the cards, you don't want to return Many decks

Upvotes: 1

Alireza
Alireza

Reputation: 10486

Your mistake is "Selecting CardNumber and assigning it to Deck"

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564861

You're selecting a CardNumber, but trying to place it into a deck:

// This won't work:
// IEnumerable<Deck> deckQuery =
IEnumerable<CardNumber> deckQuery = 
        from myCard in deck.Cards
        where myCard.Suit == Suit.Club
        select myCard.CardNumber;

The other option would be to select the card itself:

IEnumerable<Card> deckQuery = 
        from myCard in deck.Cards
        where myCard.Suit == Suit.Club
        select myCard;

Upvotes: 5

Related Questions