Home > Enterprise >  convert the strings in my list to a list c#
convert the strings in my list to a list c#

Time:07-05

i'm quite new to this, i already did a little c# in unity, but never really in VS.

I have a txt file that looks approximatively like this :

monday;8;server1,server2,server3

tuesday;9;server3,server4

wedneday;8;server1,server2,server4

i splitted this into 3 list, one with the day (monday)... one with the hour (8)... and one with the servers(server1,server2,server3), but i would like to convert this server string into unique lists,

for example i would like a list1 to contain all servers to of monday (server1,server2,server3), a list2 to contain all servers of tuesday (server3,server4).

These servers names are splitted with a comma, and i would like to split every string into a unique list

i know i was not very clear, please ask for any specifications

List<string> plages_horaires_ouverture = File.ReadAllLines(@"file.txt").ToList();


        foreach (var fileLine in plages_horaires_ouverture)
        {
            var splitLignes = fileLine.Split(new[] { ";" }, StringSplitOptions.None);
            
            listeJoursOuverture.Add(splitLignes[0]);
            listeHeuresOuverture.Add(splitLignes[1]);
            listeServeursOuverture.Add(splitLignes[2]);


        }

this code splits the txt file into 3 separate lists containing strings, and now i would like to convert every element (string) of the list "listeServeursOuverture" (sorry for the french name) into a unique list. Every element in this list looks like this "server1,server2,server4" or "server2,server3" and is separated with a comma

Any ideas? I tried many things but none worked, this question is certainly stupid, and im sorry for that

Leno

CodePudding user response:

Use List.AddRange():

List<String> servers = new List<String>();
servers.AddRange(splitLignes[2].Split(new[] { "," }, StringSplitOptions.None));

CodePudding user response:

Instead of spreading your data into different Lists, I'd have one List (or at least some kind of DataStructure. You may later find a Dictionary helpful, maybe).

In that List, I'd have instances of a Model class like for example this one:

public class MyModel
{
   public string Day {get; set;}
   public int Hour {get; set;}
   public List<string> Servers {get; set;} = new List<string>();
}

Then you can look up the Server Lists by Day as you want if I read the question correctly.

Populating may go something like this:

foreach (var fileLine in plages_horaires_ouverture)
{
    var splitLignes = fileLine.Split(new[] { ";" }, StringSplitOptions.None);
    
    listeOverture.Add(new MyModel
    {
        Day = splitLignes[0],
        Hour = int.Parse(splitLignes[1].Trim()),
        Servers.AddRange(splitLignes[2].Split(new[] { "," }, StringSplitOptions.None))
    });

    // don't need these anymore.        
    // listeJoursOuverture.Add(splitLignes[0]);
    // listeHeuresOuverture.Add(splitLignes[1]);
    // listeServeursOuverture.Add(splitLignes[2]);
}

CodePudding user response:

I'd use a dictionary here:

    List<string> plages_horaires_ouverture = File.ReadAllLines(@"file.txt").ToList();

    Dictionary<KeyValuePair<string, string>,List<string>> servers = new Dictionary<KeyValuePair<string, string>,List<string>>
    foreach (var fileLine in plages_horaires_ouverture)
    {
        var splitLignes = fileLine.Split(new[] { ";" }, StringSplitOptions.None);
        
        KeyValuePair<string, string> key = new KeyValuePair<string, string>(splitLignes[0], splitLignes[1]);
        List<string> serversForDay = splitLignes[2].Split(new[] { "," }, StringSplitOptions.None);

        servers.Add(key, serversForDay);
    }

CodePudding user response:

you can edit the names and the types as needed!
make class object to sort the data as u like, then make a method that return list of that class. this is more clear for you when you need to deal with your code later

public List<DataModel> GetListFromData()
        {

            List<string> plages_horaires_ouverture = File.ReadAllLines( @"file.txt" ).ToList();

            List<DataModel> list = new List<Dd>();
            foreach ( var fileLine in plages_horaires_ouverture )
            {
                var splitLignes = fileLine.Split(  ";" , StringSplitOptions.None );

                List<string> d = splitLignes[0].Split( "," ).ToList();
                List<string> h =splitLignes[1].Split( "," ).ToList();
                List<string> s =splitLignes[2].Split( "," ).ToList();
                list.Add( new DataModel( d , h[0] , s ) );
              
            }
            return list;
        }
        class DataModel
        {
            public DataModel( List<string> days,string hour  , List<string> servers )
            {
                Hour = hour;
                Days = days;
                Servers = servers;
            }

            public string Hour { get; set; }
            public List<string> Days { get; set; }
            public List<string> Servers { get; set; }
        }
  • Related