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 Albums
public class Album
{
...
public virtual ICollection 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.