How to use Find Method for an Object Collection
-
Hi Friends, I have one class with four properties like public class Class1 { private int m_CountryID = 0; private string m_CountryName = string.Empty; private int m_OperatorId = 0; private string m_OperatorName = string.Empty; public int CountryId { get { return m_CountryId; } set { m_CountryId = value; } } public string CountryName { get { return m_CountryName ; } set { m_CountryName = value; } } public int OperatorId { get { return m_OperatorId; } set { m_OperatorId = value; } } public string OperatorName { get { return m_OperatorName; } set { m_OperatorName = value; } } }// End of Class1 Now in another class i have collection object for the above class public Class Class2 { Class1 objClass1 = null; List lstCountries = new List(); objClass1 = new Class1(); objClass1.CountryId = 1; objClass1.CountryName = "INDIA"; objClass1.OperatorId = 1; objClass1.OperatorId = "AirTel"; lstCountries.Add(objClass1); objClass1 = new Class1(); objClass1.CountryId = 1; objClass1.CountryName = "INDIA"; objClass1.OperatorId = 2; objClass1.OperatorId = "VodaFone"; lstCountries.Add(objClass1); objClass1 = new Class1(); objClass1.CountryId = 2; objClass1.CountryName = "US"; objClass1.OperatorId = 3; objClass1.OperatorId = "US-VodaFone"; lstCountries.Add(objClass1); objClass1 = new Class1(); objClass1.CountryId = 2; objClass1.CountryName = "US"; objClass1.OperatorId = 4; objClass1.OperatorId = "US-Airtel"; lstCountries.Add(objClass1); /* now I know the operatorid. I want to get the country id of that perticular Operatorid with in the List, Eg: if i send oprtaorid 3 i wantto get countryid as 2 lstCountries.Find("How can we use this") For this one How can we use List.Find Method or any other alternative..? */ } Haribabu
-
Hi Friends, I have one class with four properties like public class Class1 { private int m_CountryID = 0; private string m_CountryName = string.Empty; private int m_OperatorId = 0; private string m_OperatorName = string.Empty; public int CountryId { get { return m_CountryId; } set { m_CountryId = value; } } public string CountryName { get { return m_CountryName ; } set { m_CountryName = value; } } public int OperatorId { get { return m_OperatorId; } set { m_OperatorId = value; } } public string OperatorName { get { return m_OperatorName; } set { m_OperatorName = value; } } }// End of Class1 Now in another class i have collection object for the above class public Class Class2 { Class1 objClass1 = null; List lstCountries = new List(); objClass1 = new Class1(); objClass1.CountryId = 1; objClass1.CountryName = "INDIA"; objClass1.OperatorId = 1; objClass1.OperatorId = "AirTel"; lstCountries.Add(objClass1); objClass1 = new Class1(); objClass1.CountryId = 1; objClass1.CountryName = "INDIA"; objClass1.OperatorId = 2; objClass1.OperatorId = "VodaFone"; lstCountries.Add(objClass1); objClass1 = new Class1(); objClass1.CountryId = 2; objClass1.CountryName = "US"; objClass1.OperatorId = 3; objClass1.OperatorId = "US-VodaFone"; lstCountries.Add(objClass1); objClass1 = new Class1(); objClass1.CountryId = 2; objClass1.CountryName = "US"; objClass1.OperatorId = 4; objClass1.OperatorId = "US-Airtel"; lstCountries.Add(objClass1); /* now I know the operatorid. I want to get the country id of that perticular Operatorid with in the List, Eg: if i send oprtaorid 3 i wantto get countryid as 2 lstCountries.Find("How can we use this") For this one How can we use List.Find Method or any other alternative..? */ } Haribabu
I don't think as per your requirement list will work.One thing you can do,You can some key value pair collection.In key you put operatior id and get the object from the value. Else you can make a static function that takes the list of objects in parameter ,property name and value to search.Then search and return the result.
Cheers!! Brij