Mahin Khan
Mahin Khan

Reputation: 776

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

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++;
            }
        } 

Upvotes: 3

Views: 93

Answers (3)

Bjoern
Bjoern

Reputation: 164

Create a class and write the values to properties. I.e.:

public class Weather
{
 public string Outlook {get;set;}
 ...
}

And then store them into a List<Weather> collection (during your loop). Like already said, you can run LINQ queries on it. The internet is full of example how to use LINQ.

Upvotes: 1

omer schleifer
omer schleifer

Reputation: 3935

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>(); 

        }

Upvotes: 3

No Idea For Name
No Idea For Name

Reputation: 11597

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

Upvotes: 1

Related Questions