views:

47

answers:

2

Hi

I have a Posts class and that post can have one file and that file can have many tags

I want to iterate through the files in a post and show all the files tags

foreach(File f in Post.Files)
{
    f.Tags
}

What do I need in this foreach to get the top tag? there will only ever be one.

i tried

f.Tags.Select(n => n)

with no luck.

Thanks

+1  A: 

To get the first tag for a file, use this:

f.Tags.First()

If you have one file per post and multiple tags per file though, something like this might be more appropriate:

foreach( Tag t in Post.Files.First().Tags ) {
    // Do something with t
}
Eric Petroelje
A: 

Posts.SelectMany(pPost => pPost.Files.SelectMany(pFile => pFile.Tags.Select(pTag => pTag)))

Vasu Balakrishnan