locked
Neat trick - user .OfType<whatever> to filter out nulls

    Question

  • If you have a collection that might possibly contain some values that are null, use the .OfType<whatever>() operator to filter out any values that are null in the collection.
    Friday, September 12, 2008 12:26 AM

Answers

  •  Xun Ye - MSFT wrote:

    Hi aemami,


    According to MSDN, Enumerable.OfType<(Of <(TResult>)>) Method and Queryable.OfType<(Of <(TResult>)>) Method, the “.OfType” method is used to filter the collections based on a specified type, and the “null” is not type specific, it can be any reference type and null-able type, so in this scenario, the “.OfType” will not work to filter the “null” values.


    Anyway, we can use the LINQ query statement to do this, please check the code snippet below.


         string[] wordlist = { "Make", "in", "Model", "7", "In", null };

                var queries = from a in wordlist

                              where (null != a)

                              select a;

     

                foreach (string q1 in queries)

                    Console.WriteLine(q1);


    Regards,

    Xun

     

    What the orginal poster was pointing out was that the OfType<T> method has a side-effect of also filtering nulls (not that it was the intended purpose of the method.)   The OfType method basically does a C# 'is' check on the elements of the sequence, and since null values never match any type they are filtered out.

    Tuesday, September 16, 2008 5:25 AM
  • Hi Matt,

    Sorry, it is my fault; I misunderstood the mechanism in “.OfType”, I have test the following code snippet, and found it works.

    System.Collections.ArrayList fruits = new System.Collections.ArrayList(4);

                fruits.Add("Mango");

                fruits.Add("Orange");

                fruits.Add("Apple");

                fruits.Add("Banana");

                fruits.Add(null);

     

                // Apply OfType() to the ArrayList.

                IEnumerable<string> query1 = fruits.OfType<string>();

     

                Console.WriteLine("Elements of type 'string' are:");

                foreach (string fruit in query1)

                    Console.WriteLine(fruit);

    Regards,

    Xun

    Tuesday, September 16, 2008 5:39 AM

All replies

  • Presumably this is for nullable value types?
    Friday, September 12, 2008 12:29 AM
  • Works for reference types as well.

    Basically any time you're working with a collection and its possible that a null value was inserted into the collection (i.e. perhaps you used the SingleOrDefault function to perform a lookup of soemthing and it failed to find the item - it would place a null in your collection).
    Friday, September 12, 2008 12:31 AM
  • Hi aemami,


    According to MSDN, Enumerable.OfType<(Of <(TResult>)>) Method and Queryable.OfType<(Of <(TResult>)>) Method, the “.OfType” method is used to filter the collections based on a specified type, and the “null” is not type specific, it can be any reference type and null-able type, so in this scenario, the “.OfType” will not work to filter the “null” values.


    Anyway, we can use the LINQ query statement to do this, please check the code snippet below.


         string[] wordlist = { "Make", "in", "Model", "7", "In", null };

                var queries = from a in wordlist

                              where (null != a)

                              select a;

     

                foreach (string q1 in queries)

                    Console.WriteLine(q1);


    Regards,

    Xun

    Tuesday, September 16, 2008 2:56 AM
  •  Xun Ye - MSFT wrote:

    Hi aemami,


    According to MSDN, Enumerable.OfType<(Of <(TResult>)>) Method and Queryable.OfType<(Of <(TResult>)>) Method, the “.OfType” method is used to filter the collections based on a specified type, and the “null” is not type specific, it can be any reference type and null-able type, so in this scenario, the “.OfType” will not work to filter the “null” values.


    Anyway, we can use the LINQ query statement to do this, please check the code snippet below.


         string[] wordlist = { "Make", "in", "Model", "7", "In", null };

                var queries = from a in wordlist

                              where (null != a)

                              select a;

     

                foreach (string q1 in queries)

                    Console.WriteLine(q1);


    Regards,

    Xun

     

    What the orginal poster was pointing out was that the OfType<T> method has a side-effect of also filtering nulls (not that it was the intended purpose of the method.)   The OfType method basically does a C# 'is' check on the elements of the sequence, and since null values never match any type they are filtered out.

    Tuesday, September 16, 2008 5:25 AM
  • Hi Matt,

    Sorry, it is my fault; I misunderstood the mechanism in “.OfType”, I have test the following code snippet, and found it works.

    System.Collections.ArrayList fruits = new System.Collections.ArrayList(4);

                fruits.Add("Mango");

                fruits.Add("Orange");

                fruits.Add("Apple");

                fruits.Add("Banana");

                fruits.Add(null);

     

                // Apply OfType() to the ArrayList.

                IEnumerable<string> query1 = fruits.OfType<string>();

     

                Console.WriteLine("Elements of type 'string' are:");

                foreach (string fruit in query1)

                    Console.WriteLine(fruit);

    Regards,

    Xun

    Tuesday, September 16, 2008 5:39 AM