How do I form a good predicate delegate to Find() something in my List?

前端 未结 6 1530
囚心锁ツ
囚心锁ツ 2020-12-23 12:37

After looking on MSDN, it\'s still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class)

6条回答
  •  我在风中等你
    2020-12-23 12:49

    Hmm. Thinking more about it, you could use currying to return a predicate.

    Func> byYear = i => (c => c.Year == i);
    

    Now you can pass the result of this function (which is a predicate) to your Find method:

    my99Car = cars.Find(byYear(1999));
    my65Car = cars.Find(byYear(1965));
    

提交回复
热议问题