0

Recently, I have been getting into C# (ASP.NET) and moving on from PHP. I want to achieve something like this:

mainArray (
   array 1 (
      'name' => 'example'
   ),
   array 2 (
      'name' => 'example2'
   )
);

I know that you can use an Array in C# however, you must indicate the length of the Array before doing so which is where the problem is.

I want to loop through a Database in a Class function which returns an Array of all the columns, ie:

id, username, email.

I have tried:

public Array search_bustype(string match, string forthat)
{
    db = new rkdb_07022016Entities2();
    var tbl = (from c in db.tblbus_business select c).ToArray();
    List<string> List = new List<string>();
    int i = 0;
    foreach (var toCheck in tbl)
    {
        if (toCheck.BusType.ToString() == match)
        {
            if (forthat == "Name")
            {
                List.Add(toCheck.Name);
            }
            if (forthat == "Address")
            {

            }
        }
        i++;
    }
    return List.ToArray();
}

But as you can see, I am having to only return the single column because the List is not multidimensional (can't be nested).

What can I use to solve this issue? I have looked at some links:

C# Arrays
StackOverflow post

But these again are an issue for my structure since I don't know how many index's I need in the Array when declaring it - The Database grows everyday.

Thanks in advance.

Community
  • 1
  • 1
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 1
    Wouldn't it be a better option to rethink this entire setup? Since you're working with data from a database, why not use models for your data to keep it structured? – Matthijs Mar 03 '16 at 09:29
  • Could you possibly reference anything on this? - I am new to C#. It would be appreciated a lot! @Matthijs – Jaquarh Mar 03 '16 at 09:30
  • 1
    You would basically create a class with the properties your data contains, and fill a list with instances of this class. Something [like this](http://stackoverflow.com/questions/16153196/how-to-parse-xml-data-into-the-properties-of-a-custom-c-sharp-class) could help. – Matthijs Mar 03 '16 at 09:33
  • Wow that makes perfect sense, so then each property is accessible through the class unless you `public` them? – Jaquarh Mar 03 '16 at 09:34
  • 1
    That depends on your class setup. I would create getter and setter methods for each property though. Take a look at auto properties in C# for that. – Matthijs Mar 03 '16 at 09:35
  • I don't think I can use the method that uses because I am using Entity Framework but I understand the structure a lot better now! Thanks @Matthijs – Jaquarh Mar 03 '16 at 09:37
  • 1
    Entity Framework makes for even better use of Models, so I would advise to use them! – Matthijs Mar 03 '16 at 09:39
  • o.O Time to do some further research for me then, appreciated! I'm looking at the link you provided right now :') @Matthijs – Jaquarh Mar 03 '16 at 09:41

2 Answers2

1

You can use a list of lists

IList<IList<string>> multiList;
Rax
  • 665
  • 8
  • 19
  • Its crazy how I spent so long trying to find this but the documentation on it clearly is named, otherwise, differently to 'multidimensional' or 'nested'. Thanks for this! – Jaquarh Mar 03 '16 at 09:39
1

Try something like this. First, define a class for your business model.

public class Person
{
    public string Name {get;set;}
    public string Address {get;set;}
}

Then use a generic list instead of a string list.

public Person[] search_bustype(string match, string forthat)
{
    var db = new rkdb_07022016Entities2();
    List<Person> personList = new List<Person>();
    foreach (var toCheck in db.tblbus_business.Where(b => b.BusType.ToString() == match))
    {
        var model = new Person { Name = toCheck.Name, Address = toCheck.Address };
        personList.Add(model);        
   }
   return personList.ToArray();
}

I'm not sure what you are trying to do with the forthat variable.

fireydude
  • 1,181
  • 10
  • 23
  • the `forthat` variable - excuse the terrible name - was just the column I wanted to pull from. ie: `search_bustype('CUS', 'Name');` would search the bus type for 'CUS' and return a `List` of `Names` corresponding to all Bus_type Customer. Thanks for this though! – Jaquarh Mar 03 '16 at 09:50