1.AnyThe operator is used to determine whether the sequence is null or whether the sequence contains a specific value.
VaR list = new list <int> {1, 2, 3,}; bool isempty = List. Any (); bool ishavemorethantwo = List. Any (A => A> 2 );
When any is used to determine whether a set contains a specified element that meets the condition, the system returns immediately when the first element that meets the condition is met, and does not judge the element after the set.
2.Contains. used to determine whether the set contains the specified Element
VaR list = new list <int> {1, 2, 3,}; var ishave = List. Contains (1 );
Contains has another overload method called
Public static bool contains <tsource> (this ienumerable <tsource> source, tsource value, iequalitycomparer <tsource> comparer );
If a custom object is saved in the list, we can use the iequalitycomparer <t> interface to customize an equal comparison rule.
For example:
Class person {public int32 ID {Get; set;} Public String fullname {Get; Set ;}} class student: person {Public String major {Get; Set ;}}
Custom comparison rules (as long as the IDs are the same, we think the two objects are the same ):
Class studenttopersonequals: iequalitycomparer <person> {public bool equals (Person X, person y) {If (X. id = y. ID) return true; else return false;} public int gethashcode (person OBJ) {return obj. gethashcode ();}}
VaR people = new list <person> (); people. add (new student () {id = 2, fullname = "Lisi"}); people. add (new student () {id = 3, fullname = "wangwu", Major = "chemistry"}); people. add (new student () {id = 4, fullname = "zhaoliu", Major = "History"}); console. writeline ("People contains zhangsan? {0} ", people. Contains (zhangsan, new studenttopersonequals ()));
3. Return the specified number of elements starting with the sequence
Returns the first three elements of a sequence.
VaR list = enumerable. Range (1, 10); var ret = List. Take (3). Select (A => );
Returns the first two elements greater than 5 in the sequence.
VaR list = enumerable. Range (1, 10); var ret = List. Where (A => A> 5). Take (2 );
4. Skip, skip the specified data element in the sequence, and return the remaining element.
Skip the first two elements and return the last eight elements.
VaR list = enumerable. Range (1, 10); var ret = List. Skip (2 );
Use skip and take together to achieve paging Effect
Collection. Skip (currentpageindex * itemsinpage). Take (itemsinpage)
5. distinct, used to return non-repeating elements in the sequence
Public class person {int32 public ID {Get; set;} Public String fullname {Get; Set ;}} public class personateitycomparer: iequalitycomparer <person> {public bool equals (Person X, person y) {return X. id = y. ID;} public int gethashcode (person OBJ) {return obj. ID ;}}
List <person> Persons = new list <person> (); persons. add (new person {fullname = "DD", id = 1,}); persons. add (new person {fullname = "DD", id = 1,}); persons. add (new person {fullname = "DD", id = 2,}); var ret = persons. distinct (New personateitycomparer (); foreach (VAR item in RET) {debug. writeline (item. ID );}
About the iequalitycomparer interface implementation,
To ensure that two objects are equal, ensure that their gethashcode is the same.