For those who are still stuck in .Net 2 ...
The array structure of my class ...
public class EmployerContactDetailsClass { public string wsStudentID { get; set;} public string wsOrgID { get; set;} public string wsContactID { get; set;} public string wsContactName { get; set;} public string wsContactEmail { get; set;} public string wsEmployerEmail { get; set;} public bool wsUserChoiceVerifier { get; set;} }
I want to find a specific element in this class based on the passed index value using wsStudentID == 'somevalue' .
First create an array of list
List<EmployerContactDetailsClass> OrgList = new List<EmployerContactDetailsClass>();
Next create one node element
EmployerContactDetailsClass OrgElem = new EmployerContactDetailsClass();
Create your list from some data source (in my case, a Reader object)
OrgElem.wsOrgID = reader["OrganisationID"].ToString(); //Org ID from database OrgElem.wsContactID = reader["ContactID"].ToString(); //Contact ID from employer database OrgElem.wsUserChoiceVerifier = (bool)reader["UserChoiceVerify"]; //boolean by default if (reader["ContactGivenName"].ToString() != string.Empty && OrgElem.wsUserChoiceVerifier) OrgElem.wsContactName = reader["ContactGivenName"].ToString() + " " + reader["ContactFamilyName"].ToString(); else OrgElem.wsContactName = "TO WHOM IT MAY CONCERN"; OrgElem.wsContactEmail = reader["ContactEmail"].ToString(); //" support@tecnq.com.au "; OrgElem.wsEmployerEmail = reader["OrganisationEmail"].ToString(); //" support@tecnq.com.au "; //now add the elements into the array class OrgList.Add(OrgElem);
Convert list <> to array []
EmployerContactDetailsClass[] OrgListArray = (EmployerContactDetailsClass[])OrgList.ToArray();
Find the specific StudentID in the array using Delegate .
String wsStudentID = 'FIND007'; //with respect to Roger Moore RIP EmployerContactDetailsClass OrgElemFound = Array.Find(OrgListArray, ByStudentID(wsStudentID));
And the Predicate method is somewhere else on your code ...
//predicate method to return true or f if studentID matches the array list (used to pass into SendEmail) private Predicate<EmployerContactDetailsClass> ByStudentID(string wsStudentID) { return delegate(EmployerContactDetailsClass OrgElem) { return OrgElem.wsStudentID == wsStudentID; }; }
Hooray!
Fandango68
source share