Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to use Find Method for an Object Collection

How to use Find Method for an Object Collection

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kuricheti
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • K Kuricheti

      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

      B Offline
      B Offline
      Brij
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups