How to search in 2D array by LINQ ?

How to search in 2D array by LINQ ?

You can use LINQ to search a two-dimensional array by flattening the array using the SelectMany method and then applying a search condition using the Where method.

Here's an example code snippet that demonstrates how to search a two-dimensional array using LINQ:

// Define a two-dimensional array int[,] array = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; // Search the array using LINQ int searchValue = 4; var searchResult = array.Cast<int>().Where(x => x == searchValue).ToList(); // Output the search result if (searchResult.Count > 0) { Console.WriteLine($"The value {searchValue} was found at index {searchResult[0]}"); } else { Console.WriteLine($"The value {searchValue} was not found in the array"); } 

In this example, a two-dimensional array array is defined and a search value searchValue is specified. The Cast method is used to flatten the two-dimensional array into a single-dimensional IEnumerable<int> sequence, and the Where method is used to search for the specified value searchValue. The ToList method is used to convert the search result to a List<int> for easier processing.

Note that the Cast method is used to convert the two-dimensional array into a IEnumerable<int> sequence, which allows you to use LINQ to search the array. The ToList method is used to convert the search result to a List<int> so that you can easily process the search result.

Examples

  1. Searching in a 2D Array using LINQ in C#:

    • Description: This query involves using LINQ to search for specific elements within a 2D array in C#.
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchElement = 5; var result = (from int item in array2D where item == searchElement select item).FirstOrDefault(); if (result != 0) { Console.WriteLine($"Element {searchElement} found in the array."); } else { Console.WriteLine($"Element {searchElement} not found in the array."); } } } 
  2. C# LINQ Query for Searching in a 2D Array by Value:

    • Description: This query demonstrates how to use LINQ queries to search for a specific value within a 2D array in C#.
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchValue = 6; var result = Enumerable.Range(0, array2D.GetLength(0)) .SelectMany(x => Enumerable.Range(0, array2D.GetLength(1)) .Where(y => array2D[x, y] == searchValue) .Select(y => new { X = x, Y = y })) .FirstOrDefault(); if (result != null) { Console.WriteLine($"Element {searchValue} found at index [{result.X}, {result.Y}] in the array."); } else { Console.WriteLine($"Element {searchValue} not found in the array."); } } } 
  3. Searching in a 2D Array with LINQ and Returning Coordinates:

    • Description: This query aims to search for an element in a 2D array using LINQ and return its coordinates (indices).
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchElement = 8; var coordinates = Enumerable.Range(0, array2D.GetLength(0)) .SelectMany(x => Enumerable.Range(0, array2D.GetLength(1)) .Where(y => array2D[x, y] == searchElement) .Select(y => new { X = x, Y = y })) .FirstOrDefault(); if (coordinates != null) { Console.WriteLine($"Element {searchElement} found at index [{coordinates.X}, {coordinates.Y}] in the array."); } else { Console.WriteLine($"Element {searchElement} not found in the array."); } } } 
  4. C# LINQ Query for Searching in a 2D Array and Getting All Matches:

    • Description: This query demonstrates how to use LINQ to search for all occurrences of a specific value within a 2D array in C#.
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchValue = 6; var coordinates = Enumerable.Range(0, array2D.GetLength(0)) .SelectMany(x => Enumerable.Range(0, array2D.GetLength(1)) .Where(y => array2D[x, y] == searchValue) .Select(y => new { X = x, Y = y })) .ToList(); if (coordinates.Any()) { Console.WriteLine($"Element {searchValue} found at the following coordinates:"); foreach (var coord in coordinates) { Console.WriteLine($"Index [{coord.X}, {coord.Y}]"); } } else { Console.WriteLine($"Element {searchValue} not found in the array."); } } } 
  5. Searching in a 2D Array with LINQ and Returning Indices:

    • Description: This query focuses on using LINQ to search for an element in a 2D array and return its indices (coordinates).
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchElement = 8; var indices = Enumerable.Range(0, array2D.GetLength(0)) .SelectMany(x => Enumerable.Range(0, array2D.GetLength(1)) .Where(y => array2D[x, y] == searchElement) .Select(y => new Tuple<int, int>(x, y))) .FirstOrDefault(); if (indices != null) { Console.WriteLine($"Element {searchElement} found at index [{indices.Item1}, {indices.Item2}] in the array."); } else { Console.WriteLine($"Element {searchElement} not found in the array."); } } } 
  6. C# LINQ Query for Searching in a 2D Array by Condition:

    • Description: This query demonstrates how to use LINQ queries to search for elements in a 2D array based on a specific condition.
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; var searchResult = Enumerable.Range(0, array2D.GetLength(0)) .SelectMany(x => Enumerable.Range(0, array2D.GetLength(1)) .Where(y => array2D[x, y] % 2 == 0) .Select(y => array2D[x, y])) .ToList(); if (searchResult.Any()) { Console.WriteLine("Elements in the array that satisfy the condition:"); foreach (var element in searchResult) { Console.WriteLine(element); } } else { Console.WriteLine("No elements in the array satisfy the condition."); } } } 
  7. Searching in a 2D Array with LINQ and Returning Matches:

    • Description: This query involves using LINQ to search for specific elements within a 2D array and return all matches found.
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchElement = 3; var matches = from int item in array2D where item == searchElement select item; if (matches.Any()) { Console.WriteLine($"Element {searchElement} found in the array."); } else { Console.WriteLine($"Element {searchElement} not found in the array."); } } } 
  8. C# LINQ Query for Searching in a 2D Array and Returning Coordinates:

    • Description: This query demonstrates how to use LINQ to search for elements in a 2D array and return their coordinates (indices).
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchElement = 6; var coordinates = from x in Enumerable.Range(0, array2D.GetLength(0)) from y in Enumerable.Range(0, array2D.GetLength(1)) where array2D[x, y] == searchElement select new { X = x, Y = y }; var firstMatch = coordinates.FirstOrDefault(); if (firstMatch != null) { Console.WriteLine($"Element {searchElement} found at index [{firstMatch.X}, {firstMatch.Y}] in the array."); } else { Console.WriteLine($"Element {searchElement} not found in the array."); } } } 
  9. Searching in a 2D Array with LINQ and Getting All Matches:

    • Description: This query focuses on using LINQ to search for all occurrences of a specific value within a 2D array in C#.
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchValue = 6; var matches = from x in Enumerable.Range(0, array2D.GetLength(0)) from y in Enumerable.Range(0, array2D.GetLength(1)) where array2D[x, y] == searchValue select new { X = x, Y = y }; if (matches.Any()) { Console.WriteLine($"Element {searchValue} found at the following coordinates:"); foreach (var match in matches) { Console.WriteLine($"Index [{match.X}, {match.Y}]"); } } else { Console.WriteLine($"Element {searchValue} not found in the array."); } } } 
  10. C# LINQ Query for Searching in a 2D Array and Returning Indices:

    • Description: This query demonstrates how to use LINQ to search for elements in a 2D array and return their indices (coordinates).
    • Code Implementation:
      using System; using System.Linq; class Program { static void Main(string[] args) { int[,] array2D = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int searchElement = 8; var indices = from x in Enumerable.Range(0, array2D.GetLength(0)) from y in Enumerable.Range(0, array2D.GetLength(1)) where array2D[x, y] == searchElement select new { X = x, Y = y }; var firstIndex = indices.FirstOrDefault(); if (firstIndex != null) { Console.WriteLine($"Element {searchElement} found at index [{firstIndex.X}, {firstIndex.Y}] in the array."); } else { Console.WriteLine($"Element {searchElement} not found in the array."); } } } 

More Tags

inputstreamreader vscodevim tradingview-api libcurl visual-studio-code avassetexportsession google-forms avplayerviewcontroller raster autoprefixer

More C# Questions

More Dog Calculators

More Statistics Calculators

More Financial Calculators

More Mixtures and solutions Calculators