New to C# (started 2 days ago actually), I have the following class:
using System.Collections; using System.Collections.Generic; using System.Linq; namespace MicrosoftReference.Linq { public class DataSets { public static IEnumerable Deck => from s in Suits() from r in Ranks() select new { Suit = s, Rank = r }; public static int DeckSize => Suits().Count() * Ranks().Count(); public static (IEnumerable, IEnumerable) Split() { return (Deck.Take(DeckSize / 2), startingDeck.Skip(DeckSize / 2)); } static IEnumerable<string> Suits() { yield return "clubs"; yield return "diamonds"; yield return "hearts"; yield return "spades"; } static IEnumerable<string> Ranks() { yield return "two"; yield return "three"; yield return "four"; yield return "five"; yield return "six"; yield return "seven"; yield return "eight"; yield return "nine"; yield return "ten"; yield return "jack"; yield return "queen"; yield return "king"; yield return "ace"; } } }
As it is, this code will not compile. In the Split
method, it is illegal to call Take
and Skip
methods because Deck is declared as an IEnumerable
when it should be an IEnumerable<T>
. From what I see from the IDE, the correct type should be IEnumerable<{string, string}>
but this syntax is illegal. This is however what the IDE is telling me when I try another syntax:
Cannot convert expression type 'System.Collections.Generic.IEnumerable<{string Suit, string Rank}>' to return type 'System.Collections.Generic.IEnumerable
What should I do ?
https://stackoverflow.com/questions/66897731/how-to-correctly-type-a-linq-request-declared-as-a-class-property April 01, 2021 at 10:35AM
没有评论:
发表评论