Convert query to array : ToArray « LINQ « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Linq;

    class Car
    {
        public string PetName;
        public string Color;
        public int Speed;
        public string Make;
        
        public override string ToString()
        {
            return string.Format("Make={0}, Color={1}, Speed={2}, PetName={3}",Make, Color, Speed, PetName);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car[] myCars = new []{
                new Car{ PetName = "A", Color = "Silver", Speed = 100, Make = "BMW"},
                new Car{ PetName = "B", Color = "Black", Speed = 55, Make = "VW"},
                new Car{ PetName = "C", Color = "White", Speed = 43, Make = "Ford"}
            };
        
            var subset = (from c in myCars 
                where c.Speed > 55 select c).ToArray<Car>();

            foreach (Car c in subset)
            {
                Console.WriteLine("{0} is going {1} MPH", c.PetName, c.Speed);
            }
        
        }
    }








22.21.ToArray
22.21.1.Calling the ToArray Operator
22.21.2.Convert query to array with ToArray
22.21.3.Use Conversion Operators ToArray
22.21.4.Generating an array of double values by first using a query expression with orderby
22.21.5.To List: convert query result to list
22.21.6.Convert query result to an Array
22.21.7.Convert query to array