Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose the Data Stucture

I have a table of data like this.

table of data

I want to perform numerous operations on it like

  • How many times was PLAY 'no' when it was sunny
  • How many times was WINDY 'true' when PLAY was 'yes'

What data structure should I use? right now I have simple array which is coming out to be one heck of a task to control.

string[,] trainingData = new string[noOfRecords,5];
        using (TextReader reader = File.OpenText("input.txt"))
        {
            int i =0;
            string text = "";
            while ((text = reader.ReadLine()) != null)
            {
                string[] bits = text.Split(' ');
                for (int j = 0; j < noOfColumnsOfData; j++)
                {
                    trainingData[i, j] = bits[j];
                }
                i++;
            }
        } 
like image 272
Mahin Khan Avatar asked Dec 31 '25 18:12

Mahin Khan


2 Answers

To widen @Doan cuong's anwer, I would use an enumarable list of objects. each object can be calle: Record and the collection can be called Table. (Table is IEnumarable).

Here is a simple example:

static void Main(string[] args)
        {
            Table table = new Table();
            int count1 = table.records.Where(r => r.Play == false && r.Outlook.ToLower() == "sunny").Count();
        }

        public class Record
        {
            public bool Play;
            public string Outlook;
        }


        public class Table
        {
            //This should be private and Table should be IEnumarable
            public List<Record> records = new List<Record>(); 

        }
like image 172
omer schleifer Avatar answered Jan 02 '26 11:01

omer schleifer


this is a highly uncomfortable question because it's about opinion more then a valid programing question. a lot of programmers would say this or that. for the table you are showing there is no problem using array as you did for simple queries as you mentioned. for more complex data and queries i would suggest you'll take the time and effort to study LINQ

like image 40
No Idea For Name Avatar answered Jan 02 '26 09:01

No Idea For Name



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!