Parse Array

S

shapper

Hello,

I have the following class ( Level is just a simple enumeration ):

public class Theme {
public Subject Subject { get; set; }
public List<Level> Levels { get; set; }
public string Note { get; set; }
}

And MyThemes is a List<Theme>

I am filling "public string[] Themes" from a form and getting:

Themes[0] = Economy|Base,Superior|Note 1
Themes[1] = Math|Base|Note 2

Now I need to fill MyThemes with these values:

for (int i = 0; i < this.Themes.Length; i++) {

this.Themes = this.Themes.Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Theme { Subject
= ???, Levels = ???, Note = ??? }).ToList();

}

I am a little bit confused about this.

For each Theme, Theme I split it using "|" and then:
- First value is Subject
- Second value are the Levels in a CSV format (needs to be converted
to a list)
- Third value is the note

Can't I integrate Linq and Split to get this instead of using more
loops?

Thanks,
Miguel
 
G

Göran Andersson

shapper said:
Hello,

I have the following class ( Level is just a simple enumeration ):

public class Theme {
public Subject Subject { get; set; }
public List<Level> Levels { get; set; }
public string Note { get; set; }
}

And MyThemes is a List<Theme>

I am filling "public string[] Themes" from a form and getting:

Themes[0] = Economy|Base,Superior|Note 1
Themes[1] = Math|Base|Note 2

Now I need to fill MyThemes with these values:

for (int i = 0; i < this.Themes.Length; i++) {

this.Themes = this.Themes.Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Theme { Subject
= ???, Levels = ???, Note = ??? }).ToList();

}

I am a little bit confused about this.

For each Theme, Theme I split it using "|" and then:
- First value is Subject
- Second value are the Levels in a CSV format (needs to be converted
to a list)
- Third value is the note

Can't I integrate Linq and Split to get this instead of using more
loops?

Thanks,
Miguel


You can't put a list of Theme objects in a string array. Besides, you
are making a list containing a single object for every iteration of the
list, so you would overwrite the previous item each time.

Make a constructor for the Theme class that takes a string, then you can
use LINQ to create a list of objects from the string array.

Something like:

public class Theme {
public Subject Subject { get; private set; }
public List<Level> Levels { get; private set; }
public string Note { get; private set; }

public Theme(string data) {
string[] items = data.Split('|');
Subject = Enum.Parse(typeof(Subject), items[0]);
Levels = new List<Level>();
foreach (string s in items[1].Split(',')) {
Levels.Add(Enum.Parse(typeof(Level), s));
}
Note = items[2];
}

}

List<Theme> = this.Themes.Select(t => new Theme(t)).ToList();
 
S

shapper

shapper said:
I have the following class ( Level is just a simple enumeration ):
  public class Theme {
    public Subject Subject { get; set; }
    public List<Level> Levels { get; set; }
    public string Note { get; set; }
  }
And MyThemes is a List<Theme>
I am filling "public string[] Themes" from a form and getting:
Themes[0] = Economy|Base,Superior|Note 1
Themes[1] = Math|Base|Note 2
Now I need to fill MyThemes with these values:
   for (int i = 0; i < this.Themes.Length; i++) {
   this.Themes = this.Themes.Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Theme { Subject
= ???, Levels = ???, Note = ??? }).ToList();

I am a little bit confused about this.
For each Theme, Theme I split it using "|" and then:
- First value is Subject
- Second value are the Levels in a CSV format (needs to be converted
to a list)
- Third value is the note

Can't I integrate Linq and Split to get this instead of using more
loops?
Thanks,
Miguel

You can't put a list of Theme objects in a string array. Besides, you
are making a list containing a single object for every iteration of the
list, so you would overwrite the previous item each time.

Make a constructor for the Theme class that takes a string, then you can
use LINQ to create a list of objects from the string array.

Something like:

public class Theme {
    public Subject Subject { get; private set; }
    public List<Level> Levels { get; private set; }
    public string Note { get; private set; }

    public Theme(string data) {
       string[] items = data.Split('|');
       Subject = Enum.Parse(typeof(Subject), items[0]);
       Levels = new List<Level>();
       foreach (string s in items[1].Split(',')) {
          Levels.Add(Enum.Parse(typeof(Level), s));
       }
       Note = items[2];
    }

}

List<Theme> = this.Themes.Select(t => new Theme(t)).ToList();


Thanks! It worked fine ... just needed some casting:
Subject = (Subject)Enum.Parse(typeof(Subject), items[0]);
and
Levels.Add((Level)Enum.Parse(typeof(Level), s));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top