stijn.aerts
stijn.aerts

Reputation: 6206

Trouble accessing IEnumerable in Linq

I have following code in visual studio 2013:

IEnumerable<Brouwer> brouwers = Bierhalle.GetBrouwers();

Every Brouwer has a list of beers.

Now I need to get that list from every Brouwer, and print that list.

So I have this to print out the beers:

private static void PrintBieren(IEnumerable<Bier> bieren)
        {
            foreach (Bier b in bieren)
                Console.WriteLine(b.Naam + " - " + b.AlcoholPercentage);
        }

But how can I get that list of beers? And how can I order it by name?

I have this now but this yields an IList and I need IEnumerable:

IEnumerable<Bier> bieren = from b in brouwers
                select b.Bieren;

Upvotes: 0

Views: 50

Answers (1)

JLRishe
JLRishe

Reputation: 101768

You can do this to get the beers ordered by name:

IEnumerable<Bier> bieren = brouwers.SelectMany(b => b.Bieren)
                                   .OrderBy(bi => bi.Naam);

or using the query comprehension style that you have there:

IEnumerable<Bier> bieren = from brouwer in brouwers
                           from bier in brouwer.Bieren
                           orderby bier.Naam
                           select bier;

Upvotes: 3

Related Questions