In C# while using Linq, we have .ToArray() extension method that is used to convert IEnumerable to an array. .ToArray() forces immediate query execution and stores query result in an array.
Example of Linq .ToArray()
public partial class LinqToArray : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var carslist =
new List
{ new Cars { Company = "Maruti Suzuki", Average = 15.2 },
new Cars { Company = "Mercedes", Average = 18.7 },
new Cars { Company = "Volvo", Average = 20.0 },
new Cars { Company = "Hundai", Average = 12.8 } };
var companies = carslist.Where(c => c.Average > 15)
.Select(c => c.Company)
.ToArray();
foreach (var company in companies)
{
Console.WriteLine(company);
}
}
public class Cars
{
public string Company { get; set; }
public double Average { get; set; }
}
Output: /* //Maruti Suzuki // Mercedes // Volvo */
In above sample code we have converted List to array using linq.