I have a collection called Albums
with objects from the class Album
. This class has a property Songs
, which is a collection of Song
objects. Each Song
has an unique Id.
public IQueryable<Album> Albums
public class Album
{
...
public virtual ICollection<Song> Songs { get; set; }
...
}
public class Song
{
public int Id { get; set; }
...
}
Is it possible, using Linq, to find a Song in the Album collection? I have no idea how, I am new to Ling. I tried a bit:
Albums.FirstOrDefault(a => a.Songs.Id == id);
Thanks a lot,
Vincent
Albums.SelectMany(a=>a.Songs).FirstOrDefault(song => song.Id == id)
The SelectMany will create a flattened list of all songs for all albums allowing you to then select the first with the appropriate id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With