Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 710487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:37:33+00:00 2026-05-14T04:37:33+00:00

I am working with multi-dimentioned arrays of bool , int , and various struct

  • 0

I am working with multi-dimentioned arrays of bool, int, and various struct. The code loops through these arrays and performs some operation on specific values. For instance,

        for (int x = 0; x < this.Size.Width; x++) {
            for (int y = 0; y < this.Size.Height; y++) {
                if (this.Map[x, y]) {
                    DrawTerrain(this.Tile[x, y].Location, Terrain.Water);
                }
            }
        }

I can do simple LINQ stuff, but I can’t do what I would like. What I would like to do is use LINQ. Maybe something like

from x in this.Map where x == true execute DrawTerrain(...)

But, I don’t understand how I can get the x and y locations or how to call a method in the LINQ statement.

Also, it would be great if I can put this code into a function and be able to call it with a delegate or a predicate? I don’t know if delegate or predicate are the correct words.

    void Draw(Delegate draw, bool[,] map, struct[,] tiles) 
        from x in map where x == true draw(titles[x,y]).invoke;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:37:34+00:00Added an answer on May 14, 2026 at 4:37 am

    The LINQ query syntax isn’t really designed for working with 2-dimensional data structures, but you can write an extension method that will convert the 2D array into a sequence of values that contain the coordinates in the original 2D array and the value from the array. You’ll need a helper type to store the data:

    class CoordinateValue<T> {
      public T Value { get; set; }
      public int X { get; set; }
      public int Y { get; set; }
    }
    

    Then you can write the extension method (for any 2D array) like this:

    IEnumerable<CoordinateValue<T>> AsEnumerable(this T[,] arr) {
      for (int i = 0; i < arr.GetLength(0); i++)
        for (int j = 0; j < arr.GetLength(0); j++)
          yield new CoordinateValue<T> { Value = arr[i, j]; X = i; Y = j; };
    }
    

    Now you can finally start using LINQ. To get coordinates of all elements from the 2D array such that the value stored in the element is true, you can use the following:

    var toDraw = from tile in this.Map.AsEnumerable()
                 where tile.Value == true
                 select tile;
    
    // tile contains the coordinates (X, Y) and the original value (Value)
    foreach(var tile in toDraw)
        FillTile(tile.X, tile.Y);
    

    This lets you specify some interesting conditions when filtering the tiles. For example if you wanted to get only diagonal elements, you could write:

    var toDraw = from tile in this.Map.AsEnumerable()
                 where tile.Value == true && tile.X == tile.Y
                 select tile;
    

    To answer your second question – if you want to encapsulate the behavior into the method, you’ll probably need to use some Action<...> delegate to represent the drawing function that will be passed to the method. It depends on the type signature of your drawing method though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on multi-threading using the following codes in XCode4: #import <Foundation/Foundation.h> bool
I am currently working with multi-lingual site. In some cases i need to save
I'm working with some multi-gigabyte text files and want to do some stream processing
I've had a working version of multi-threaded code, however I was unsatisfied with my
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
I'm working on a multi-threaded Silverlight application. The application has two threads: Main/UI and
I'm working on a multi-monitor kiosk application that needs to run full-screen on both
I'm working on a multi-lingual WPF project that will be localized into many different
I'm working on a multi-threaded win32 MFC application. We are rendering a map and
I’m working on a multi dimensions array but i have a problem Imagine I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.