I 've been looking for help on how to find
Objects in generics with list. Find () method... and... take a look
What I have found.
In the follow example, I created a simple class:
Public class person
{
Private int _ id;
Private string _ name;
Public int ID {get {return _ id;} set {_ id = value ;}}
Public int name {get {return _ name;} set {_ name = value ;}}
Public Person (int id, string name)
{
_ Id = ID;
_ Name = Name;
}
}
In
The example, there's a simple class with two private attributes. Now
We're re going to create a typed list of this object and take advantage
The find () method
Public void createandsearchlist ()
{
// Create and fill the collection
List <person> mylist = new list <person> ();
Mylist. Add (new person (1, "andreysanches "));
Mylist. Add (new person (2, "alexandretarifa "));
Mylist. Add (new person (3, "emersonfacunte "));
// Find a specific object
Person mylocatedobject = mylist. Find (delegate (person p) {return P. ID = 1 ;});
}
Note: This method is often used for searching elements in list and array sets. The main technology is generic delegation.
Note: if an object is added, a new object must be added. See the following example:
Person P = new pewson ();
For (INT I = 0; I <5; I ++)
{
P. ID = I;
P. Name = "XXXX ";
List. Add (P );
}
For (INT I = 0; I <5; I ++)
{
Person P = new person ();
P. ID = I;
P. Name = "XXXX ";
List. Add (P );
}
Upper
Is there a difference between the two sides? The value of the output list is different. The first list stores the same result and the last one is the same person object. The second list achieves the expected effect.
Result: different objects are stored. Why? The reason is simple. All objects stored in a list object are referenced by a common object, so the last changed value prevails.
Object sorting: This article mainly describes how to sort the list storing datatable by tablename.
1. Create a New Sort class
Public class scenesort: icomparer <datatable>
{
Public int compare (datatable obj1, datatable obj2)
{
Int tablenamelength1;
Int tablenameleng2;
If (obj1 = NULL)
{
If (obj2 = NULL)
Return 0;
Else
Return-1;
}
Else
{
If (obj2 = NULL)
Return 1;
Else
{
Tablenamelength1 = obj1.tablename. length;
Tablenamelength1 = obj2.tablename. length;
If
(Convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2)> convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2 )))
Return 1; // return 1 if the value is greater
Else if
(Convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2) <convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2 )))
Return-1; // less than-1
Else
Return 0; // return 0 if they are equal
}
}
}
2 sorting:
List <datatable> LDT = new list <datatable> ();
Scenesort Ss = new scenesort ();
Tablelist. Sort (SS); to sort the list;
I 've been looking for help on how to find
Objects in generics with list. Find () method... and... take a look
What I have found.
In the follow example, I created a simple class:
Public class person
{
Private int _ id;
Private string _ name;
Public int ID {get {return _ id;} set {_ id = value ;}}
Public int name {get {return _ name;} set {_ name = value ;}}
Public Person (int id, string name)
{
_ Id = ID;
_ Name = Name;
}
}
In
The example, there's a simple class with two private attributes. Now
We're re going to create a typed list of this object and take advantage
The find () method
Public void createandsearchlist ()
{
// Create and fill the collection
List <person> mylist = new list <person> ();
Mylist. Add (new person (1, "andreysanches "));
Mylist. Add (new person (2, "alexandretarifa "));
Mylist. Add (new person (3, "emersonfacunte "));
// Find a specific object
Person mylocatedobject = mylist. Find (delegate (person p) {return P. ID = 1 ;});
}
Note: This method is often used for searching elements in list and array sets. The main technology is generic delegation.
Note: if an object is added, a new object must be added. See the following example:
Person P = new pewson ();
For (INT I = 0; I <5; I ++)
{
P. ID = I;
P. Name = "XXXX ";
List. Add (P );
}
For (INT I = 0; I <5; I ++)
{
Person P = new person ();
P. ID = I;
P. Name = "XXXX ";
List. Add (P );
}
Upper
Is there a difference between the two sides? The value of the output list is different. The first list stores the same result and the last one is the same person object. The second list achieves the expected effect.
Result: different objects are stored. Why? The reason is simple. All objects stored in a list object are referenced by a common object, so the last changed value prevails.
Object sorting: This article mainly describes how to sort the list storing datatable by tablename.
1. Create a New Sort class
Public class scenesort: icomparer <datatable>
{
Public int compare (datatable obj1, datatable obj2)
{
Int tablenamelength1;
Int tablenameleng2;
If (obj1 = NULL)
{
If (obj2 = NULL)
Return 0;
Else
Return-1;
}
Else
{
If (obj2 = NULL)
Return 1;
Else
{
Tablenamelength1 = obj1.tablename. length;
Tablenamelength1 = obj2.tablename. length;
If
(Convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2)> convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2 )))
Return 1; // return 1 if the value is greater
Else if
(Convert. toint32 (obj1.tablename. substring (2, tableNameLength1-2) <convert. toint32 (obj2.tablename. substring (2, tableNameLength2-2 )))
Return-1; // less than-1
Else
Return 0; // return 0 if they are equal
}
}
}
2 sorting:
List <datatable> LDT = new list <datatable> ();
Scenesort Ss = new scenesort ();
Tablelist. Sort (SS); to sort the list;