LINQ with Array and ArrayList – Search Array using Lambda Expression

In this tutorial, you will learn:
  1. How to Search or Print Array and ArrayList using LINQ (Lambda Expression) C#.

LINQ with Array

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.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqTutorial
{     
    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;
            //Method 2
            //var search = productList.Where(p => p.Contains("Disk"));
            
            foreach (var result in search)
            {
                Console.WriteLine("Product Name: {0}", result);
            }

            Console.ReadKey();
        }
    }
}

Output:


Product Name: Hard Disk, Price: 1280
Product Name: SSD Disk, Price: 3500
_

LINQ with ArrayList

ArrayList is the part of System.Collections namespace and it allows to dynamically create array. You don't need to specify array size unlike traditional array and the size grows as you will add more element into it. However, it is slower than Array but it is more useful when you work with collection. Here, in this example, we will learn how to process data in ArrayList using LINQ C#.

ArrayList Example

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace LinqTutorial
{     
    class Program
    {
        class ProductStore
        {
            public string productName { get; set; }
            public int productPrice { get; set; }         
        }
        static void Main(string[] args)
        {
            ArrayList productList = new ArrayList();
            productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 });
            productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 });
            productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 });
            productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 });
            productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 });
            productList.Add(new ProductStore { productName = "Keyboard", productPrice = 1130 });

            //Method 1: Query Expression
            var search = from ProductStore p in productList
                         where p.productName.Contains("Disk")
                         select p;

            
            
            foreach (var result in search)
            {
                Console.WriteLine("Product Name: {0}, Price: {1}", result.productName, result.productPrice);
            }

            Console.ReadKey();
        }
    }
}

Output

Product Name: Hard Disk, Price: 1280
Product Name: SSD Disk, Price: 3500

_

Summary

In this tutorial, I have tried to explain Array and ArrayList implementation using LINQ C#. In the next chapter, you will learn LINQ with Dictionary.

 

Share your thought