New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

OfType Linq Test

44 Views
Copy Code Show/Hide Line Numbers
class Program
{
    static void Main(string[] args)
    {
        List<Person> people = new List<Person>();
        people.Add(new Person() { FirstName = "John", LastName = "Doe"});
        people.Add(new Person() { FirstName = "Jane", LastName = "Doe" });
        people.Add(new Employee() { FirstName = "John", LastName = "Smith" });
 
        // Displays 3
        Console.WriteLine("Number of People In Collection:     {0}", people.Count);
 
        // Displays 1
        Console.WriteLine("Number of Employees In Collection:  {0}", people.OfType<Employee>().Count());
        Console.ReadKey();
    }
}
 
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
 
public class Employee : Person
{
    public int EmployeeId { get; set; }
}
by JamesEggers
  April 21, 2010 @ 6:03am
Tags:
Comment:
Just a quick test to see if OfType<T> does a cast or just pulls straight from how the objects were put into the collection of baseclasses.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate