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.

8
  • 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? Commented Mar 3, 2016 at 9:29
  • Could you possibly reference anything on this? - I am new to C#. It would be appreciated a lot! @Matthijs Commented Mar 3, 2016 at 9: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 could help. Commented Mar 3, 2016 at 9:33
  • 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. Commented Mar 3, 2016 at 9:35
  • 1
    Entity Framework makes for even better use of Models, so I would advise to use them! Commented Mar 3, 2016 at 9:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
1

You can use a list of lists

IList<IList<string>> multiList;

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.