Flexabust Bergson
Flexabust Bergson

Reputation: 762

How to get all elements from linq query?

I am currently testing my database relations using Entity Framework. I'm having trouble returning every elements from a linq query. The idea is that I have a table Web_Profiles that has a many-to-many relation with Web_Categories and knowing that I have an ID from both tables, using the Profiles I have, I can find a Category. Since there is an ICollection of Web_Categories in Web_Profiles I can find it using:

var idCategory = context.WebProfiles
                .Where(c => c.IDProfile == item)
                .Select(c => c.Categories)
                .ToList();

WebProfiles being a DbSet from my context and Categories is an ICollection as described above. The return type of this query is : List<ICollection<Web_Categories>>. Now when I loop through this list, if I want to retrieve the Categories associated to profiles, I only managed to retrieve one category for one profile, whereas there are many categories for many profiles. How can I do this?

This is what I have tried, making a Select of IDCategory property, but I don't know which method to use after the Select statement, to show every Category:

foreach (var id in idCategory)
{
    Console.WriteLine(id.Select(c => c.IDCategorie));
}

Thanks for your help!

Upvotes: 3

Views: 9923

Answers (1)

Dmitry Pavlushin
Dmitry Pavlushin

Reputation: 612

You can use id.ToList().ForEach(c => Console.WriteLine(c.IDCategorie))

Upvotes: 2

Related Questions