C# ArrayList Search using Linq Extension OfType Method

C# ArrayList Search using Linq Extension OfType Method

Linq OfType Method. You can add any type of object into ArrayList, in other word, array list can hold any data type. Here we will see how to filter list of similar data types from ArrayList. Let’s see VB.net and C#.net Linq OfType method.

  1. Linq Chapter 1 – Split string to list of strings.
  2. Linq Chapter 2 – Remove duplicates from list.
  3. Linq Chapter 3 – Linq Where Clause.
  4. Linq Chapter 4 – Linq Group by multiple clause.
  5. Linq Chapter 5 – Linq Inner Join query.
  6. Linq Chapter 6 – Linq Distinct list.
  7. Linq Chapter 7 – Linq OfType Method.

For Demo purpose we have two classes UserProfile and Geography. Both are of different type. We have created their objects and added them into ArrayList objects.

void Main()
{

    var userprofileList = new UserProfile { FirstName = "Satalaj" , LastName = "More" };
    var userprofileList1 = new UserProfile { FirstName = "Sony" , LastName = "More" };
    var countryList = new Country{ Population= 105, City = "Pune"};

    var arrlist = new ArrayList();

    arrlist.Add(userprofileList);
    arrlist.Add(countryList);
    arrlist.Add(userprofileList1);

    var filteredList = arrlist.OfType<UserProfile>();

        foreach( UserProfile x in filteredList )
        {
  
               Console.WriteLine( x.FirstName );
 
        }     
}

    public class UserProfile
    {
          public string FirstName { get;set; }
          public string LastName { get;set; }
    }

    public class Country
    {    
          public int Population { get;set; }
          public string City { get;set; }
    }
Output
 
 Satalaj
 Sony

VB.net Linq OfType filter ArrayList

Let's see how to use OfType extension in VB.net.

Private Sub Main()

	Dim userprofileList = New UserProfile() With { _
		Key .FirstName = "Satalaj", _
		Key .LastName = "More" _
	}
	Dim userprofileList1 = New UserProfile() With { _
		Key .FirstName = "Sony", _
		Key .LastName = "More" _
	}
	Dim countryList = New Country() With { _
		Key .Population = 105, _
		Key .City = "Pune" _
	}

	Dim arrlist = New ArrayList()

	arrlist.Add(userprofileList)
	arrlist.Add(countryList)
	arrlist.Add(userprofileList1)

	Dim filteredList = arrlist.OfType(Of UserProfile)()

	For Each x As UserProfile In filteredList


		Console.WriteLine(x.FirstName)
	Next
End Sub

Public Class UserProfile
	Public Property FirstName() As String
		Get
			Return m_FirstName
		End Get
		Set
			m_FirstName = Value
		End Set
	End Property
	Private m_FirstName As String
	Public Property LastName() As String
		Get
			Return m_LastName
		End Get
		Set
			m_LastName = Value
		End Set
	End Property
	Private m_LastName As String
End Class

Public Class Country
	Public Property Population() As Integer
		Get
			Return m_Population
		End Get
		Set
			m_Population = Value
		End Set
	End Property
	Private m_Population As Integer
	Public Property City() As String
		Get
			Return m_City
		End Get
		Set
			m_City = Value
		End Set
	End Property
	Private m_City As String
End Class

Tags:

c# arraylistHow to select multiple columns in Linq- C#.net VB.net codeLinq ToDictionary ExtensionLinq Group By Multiple ColumnsLinq Split String To ListC# Parse method of DateTimeLinq Distinct ListLinq Inner JoinLearn Linq Step By StepLinq Union and intersect ExampleC# Arraylist foreach

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.