gpt4 book ai didi

C#/LINQ 查询类

转载 作者:太空宇宙 更新时间:2023-11-03 19:13:12 25 4
gpt4 key购买 nike

我正在使用 Sam Jenkin's Deck of Cards class .我正在尝试查询套装中的某些卡片。我收到错误:

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

我试过四处移动东西,但我不明白这个错误。有人可以帮忙吗?我的代码是:

var deck = new Deck();

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

我的 Card 类是:

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

我的枚举数是:

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,
}

最佳答案

您正在选择一个 CardNumber,但试图将它放入一副牌中:

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

另一种选择是选择卡片本身:

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

关于C#/LINQ 查询类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18992248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com