linq判断集合中相同元素个数_Linq:在集合中查找元素

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值