0

Could you tell me how can I put elements from a list to twodimensional array in C#?

tura = new string[howManyWords, 6];
    howManyWords = 0;

    foreach (words word in wc.words)
    {
        if (word.CategoryNumber == scb.Category)
        {
            tura[howManyWords] ......; howManyWords++;
        }
    }

I have a list made from XML file. I work in Unity.

   <?xml version="1.0" encoding="utf-8"?>
<WordCollection>
  <Words>
    <Word name="$mezczyzna">
      <CategoryNumber>1</CategoryNumber>
      <PolishName>mężczyzna</PolishName>
      <EnglishName>a man</EnglishName>
      <AudioName>man.mp3</AudioName>
      <ImageName>man</ImageName>
      <ImageLocalisationWidth>200</ImageLocalisationWidth>
      <ImageLocalisationHigh>700</ImageLocalisationHigh>
    </Word>
  </Words>
</WordCollection>
Quicki
  • 391
  • 1
  • 5
  • 15
  • 3
    This doesn't look like a two dimensional array to me. This looks like a List of Objects of type Word. Why are you trying to make it a 2d array, and a 2d array of what? What are you expecting the new array to look like? – Dispersia Nov 08 '16 at 17:11
  • There is a built-in `List.ToArray()` method: https://msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx that you may be able to use. – dub stylee Nov 08 '16 at 17:11
  • @dubstylee that is not a 2d array. – Dispersia Nov 08 '16 at 17:11
  • duplicate of http://stackoverflow.com/questions/11295746/converting-listt-to-array-multidimensional – Anwar Ul-haq Nov 08 '16 at 17:13
  • 1
    I think the user wants a dictionary but doesn't know it. – Hogan Nov 08 '16 at 17:14
  • I create a lost from long XML file which include data about words, and their creature in interface. I have a few levels (categories). I'd like to have access to only these parts of these data which I need in particular moment – Quicki Nov 08 '16 at 17:35
  • Is this based off of CategoryNumber then? Like a CategoryNumber aligns with a certain Category? – Dispersia Nov 08 '16 at 23:05

1 Answers1

1

Since we believe you mean by dictionary, and by the ContentNumber, here is an example:

void Main()
{
    List<Word> Words = new List<UserQuery.Word> { new Word { CategoryNumber = 0, EnglishName = "WORD1" }, new Word { CategoryNumber = 1, EnglishName = "WORD2" }, new Word { CategoryNumber = 1, EnglishName = "WORD3" } };

    Dictionary<CategoryTypes, List<Word>> categoryWords = Words.GroupBy(word => word.CategoryNumber).ToDictionary(x => (CategoryTypes)x.Key, x => x.ToList());

    if (categoryWords.ContainsKey(CategoryTypes.Category2))
    {
        List<Word> words = categoryWords[CategoryTypes.Category2];

        for (int i = 0; i < words.Count; i++)
        {
            Console.WriteLine(words[i].EnglishName);
        }
    }
}

class Word
{
    public int CategoryNumber { get; set; }
    public string EnglishName { get; set; }
}

enum CategoryTypes
{
    Category1 = 0,
    Category2 = 1
}

You can then call the dictionary categoryWords by the type of enum, and then only have your list of words.

Dispersia
  • 1,436
  • 9
  • 23