Create IEnumerable.Find()

后端 未结 3 1884
故里飘歌
故里飘歌 2021-02-07 01:39

I\'d like to write:

IEnumerable cars;
cars.Find(car => car.Color == \"Blue\")

Can I accomplish this with extension methods? The f

3条回答
  •  悲&欢浪女
    2021-02-07 02:06

    You know that Find(...) can be replaced by Where / First

    IEnumerable cars;
    var result = cars.Where(c => c.Color == "Blue").FirstOrDefault();
    

    This'll return null in the event that the predicate doesn't match.

提交回复
热议问题