How to extract the integers from a list of objects using Linq in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;
  
class Program
{
    static void Main() {
        var list = new List<object> { 3, 7, "c#", 8, "c", 99, 10, "c++", -5, 1, "java", 2 };

        List<int> result = list.OfType<int>().ToList();

        Console.WriteLine(String.Join(", ", result));
    }
}
  
  
  
/*
run:
     
3, 7, 8, 99, 10, -5, 1, 2
   
*/
      

 



answered Sep 27, 2023 by avibootz
...