Searching if value exists in a list of objects using Linq

后端 未结 9 1667
执念已碎
执念已碎 2020-12-04 05:17

Say I have a class Customer which has a property FirstName. Then I have a List.

Can LINQ be used to find if th

9条回答
  •  爱一瞬间的悲伤
    2020-12-04 05:53

    LINQ defines an extension method that is perfect for solving this exact problem:

    using System.Linq;
    ...
        bool has = list.Any(cus => cus.FirstName == "John");
    

    make sure you reference System.Core.dll, that's where LINQ lives.

提交回复
热议问题