LINQ to Int Array

LINQ to int array means writing LINQ queries on an integer array to get required elements from integer array elements. Using LINQ queries on integer arrays, we can get the required data from the integer array without writing much code. 

Syntax of LINQ to Int Array

Following is the syntax of writing LINQ queries on integer arrays to get the required elements from array collection.

 

C# Code

 

IEnumerable<int> result = from a in numarr
                          select a;

VB.NET Code

 

Dim result As IEnumerable(Of Integer) = From a In numarr

If you observe the above syntax, we wrote the LINQ query to get data from the “numarr” integer array.

Example of LINQ to Int Array

Following is the example of LINQ to Int Array to get elements from the sequence where elements value greater than 10 and less than 200.

 

C# Code

 

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

namespace Linqtutorials
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] numarr = { 1, 6, 9, 10, 50, 60, 100, 200, 300 };
      IEnumerable<int> result = from a in numarr where a > 10 && a < 200
                                select a;
      foreach (int item in result)
      {
        Console.WriteLine(item);
      }
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim numarr As Integer() = {1, 6, 9, 10, 50, 60, 100, 200, 300}
Dim result As IEnumerable(Of Integer) = From a In numarr Where a > 10 AndAlso a < 200
For Each item As Integer In result
Console.WriteLine(item)
Next
Console.ReadLine()
End Sub
End Module

If you observe the above code, we used the LINQ query on the “numarr” integer array to get elements whose value is greater than 10 and less than 200.

Result of LINQ to Int Array Example

Following is the result of the LINQ to Int Array Example.

 

50
60
100

This is how we can use LINQ queries with integer arrays to get the required data in c#, vb.net, with example.