In this example, we’ll learn How to print arraylist in C# Linq.
An array is a collection of items and you can access array items using their index number. There are several ways to process data in array and you can use loop constructs to traverse array. Here, in this tutorial we will learn how to use LINQ Syntax to process data in array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp17 { class Program { static void Main(string[] args) { string[] productList = new string[7]; productList[0] = "Hard Disk"; productList[1] = "Monitor"; productList[2] = "SSD Disk"; productList[3] = "RAM"; productList[4] = "Processor"; productList[5] = "Bluetooth"; productList[6] = "Keyboard"; //Method 1 var search = from x in productList select x; foreach (var result in search) { Console.WriteLine("Product Name: {0}", result); } Console.ReadKey(); } } } |
if we want to filter data by “disk“, we must use contains method.
Here is filter example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp17 { class Program { static void Main(string[] args) { string[] productList = new string[7]; productList[0] = "Hard Disk"; productList[1] = "Monitor"; productList[2] = "SSD Disk"; productList[3] = "RAM"; productList[4] = "Processor"; productList[5] = "Bluetooth"; productList[6] = "Keyboard"; //Method 1 var search = from x in productList where x.Contains("Disk") select x; foreach (var result in search) { Console.WriteLine("Product Name: {0}", result); } Console.ReadKey(); } } } |