samox
samox

Reputation: 133

Make an IEnumerable from class

I have two classes "Word.cs" and "TrainerFile.cs". The TrainerFile contains a collection of 5 different List<Word>, and a Word contains the 2 strings Lang1 and Lang2. What I need is a List or enum for all the words inside a TrainerFile, so that i can make a random list of the words of said TrainerFile. I have no idea how to access all the words. Here is the 2 classes:

public class Word
{
    public string Lang1 { get; set; }
    public string Lang2 { get; set; }

    public Word()
    {
        this.Lang1 = "";
        this.Lang2 = "";
    }

    public Word(string lang1, string lang2)
    {
        this.Lang1 = lang1;
        this.Lang2 = lang2;
    }
}

public class TrainerFile
{
    public List<Word> FolderInitial { get; set; }
    public List<Word> Folder1 { get; set; }
    public List<Word> Folder2 { get; set; }
    public List<Word> Folder3 { get; set; }
    public List<Word> FolderFinal { get; set; }

    public TrainerFile()
    {
        this.FolderInitial = new List<Word>();
        this.Folder1 = new List<Word>();
        this.Folder2 = new List<Word>();
        this.Folder3 = new List<Word>();
        this.FolderFinal = new List<Word>();
    }

    public TrainerFile(List<Word> listInitial, List<Word> list1, List<Word> list2, List<Word> list3, List<Word> listFinal)
    {
        this.FolderInitial = listInitial;
        this.Folder1 = list1;
        this.Folder2 = list2;
        this.Folder3 = list3;
        this.FolderFinal = listFinal;
    }
}

Upvotes: 1

Views: 96

Answers (2)

Gilad Green
Gilad Green

Reputation: 37281

Use linq's Concat:

var result = FolderInitial.Concat(Folder1)
                          .Concat(Folder2)
                          .Concat(Folder3)
                          .Concat(FolderFinal);
  • If you want a distinct list then when instead of using Concat use Union and override the Word class's Equals and GetHashCode methods to compare by the properties of the class.
  • Another way instead of overriding the methods is supplying an IEqualityComparer of this overload of Union

Also, since C# 6.0, instead of having the default constructor you can:

public class TrainerFile
{
    public List<Word> FolderInitial { get; set; } = new List<Word>();
    public List<Word> Folder1 { get; set; } = new List<Word>();
    public List<Word> Folder2 { get; set; } = new List<Word>();
    public List<Word> Folder3 { get; set; } = new List<Word>();
    public List<Word> FolderFinal { get; set; } = new List<Word>();
}

Upvotes: 4

maraaaaaaaa
maraaaaaaaa

Reputation: 8193

Looks like a case for a Dictionary<string, List<Word>>

Dictionary<string, List<Word>> trainerFile = new Dictionary<string, List<Word>>();
trainerFile.Add("FolderInitial", new List<Word>());
trainerFile.Add("Folder1", new List<Word>());
trainerFile.Add("Folder2", new List<Word>());
trainerFile.Add("Folder3", new List<Word>());
trainerFile.Add("FolderFinal", new List<Word>());

Then access like so:

List<string> initial = trainerFile["FolderInitial"];
trainerFile["Folder2"].Add(new Word());
Word word = trainerFile["Folder2"][0];

To access every single word,

IEnumerable<Word> all = trainerFile.Values.SelectMany(i => i);

Upvotes: -1

Related Questions