convert the strings in my list to a list c# - c#

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

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

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);
}

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);
var model = new MyModel{
Day = splitLignes[0],
Hour = int.Parse(splitLignes[1].Trim())
};
model.Servers.AddRange(splitLignes[2].Split(new[] { "," }, StringSplitOptions.None));
listeOverture.Add(model);
// don't need these anymore.
// listeJoursOuverture.Add(splitLignes[0]);
// listeHeuresOuverture.Add(splitLignes[1]);
// listeServeursOuverture.Add(splitLignes[2]);
}

You can edit the names and the types as needed. Make a class object to sort the data as you like, then make a method that returns a 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

Output data to CSV specific columns from Dictionary c#

I am trying to output the values from the dictionary to the CSV and am able to do this. But facing issue with the specific columns this need to output to the csv. I need the specific data value from dictionary to be output to a specific column in csv.
Dictionary<string, List<string>> file = new Dictionary<string, List<string>>();
for (var i = 0; i < stringList.Count(); i++)
{
string line = stringList[i];
string path = line.Replace("\r\n", "");
path = path.Replace(" ", "");
path = path.TrimEnd(':');
if (File.Exists(path))
{
file[path] = file.ContainsKey(path) ? file[path] : new List<string>();
for (var j = i + 1; j < stringList.Count(); j++)
{
string line2 = stringList[j];
string path2 = line2.Replace("\r\n", "");
path2 = path2.Replace(" ", "");
path2 = path2.TrimEnd(':');
if (File.Exists(path2))
{
i = j - 1;
break;
}
else
{
if (path2.Contains("Verified") | path2.Contains("Algorithm"))
{
var strings = path2.Split(':');
var listValue = strings[1].Trim();
file[path].Add(listValue);
}
}
}
}
}
using (var writer = new StreamWriter(outputdir + "\\output_" +
DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".csv"))
{
writer.WriteLine("FilePath,Signature,HashValueSHA1, HashValueSHA2, HashValueMD5, Other");
foreach (var keyvaluepair in file)
{
if (!keyvaluepair.Value.Contains("Unsigned"))
{
var values = String.Join(",", keyvaluepair.Value.Distinct().Select(x => x.ToString()).ToArray());
writer.WriteLine("{0},{1}", keyvaluepair.Key, values);
}
}
}
Current Output looks like below:
Sample output I need as below:
The Dictionary key(string) would hold the file path and the values(List) would hold something like below:
Signed
sha1RSA
md5RSA
md5RSA
Signed
sha1RSA
sha1RSA
sha256RSA
sha256RSA
Please suggest how can I get the one as required output.
I had a longer answer typed, but I see the problem.
On this line
var values = String.Join(",", keyvaluepair.Value.Distinct().Select(x => x.ToString()).ToArray());
take out Distinct. It looks like you have the correct number of items in each string, but if a list contains multiple blank entries Distinct is eliminating the duplicates. If a list contains two or three blank entries you need all of them. If you delete duplicate blanks your columns won't line up.
Also, when you use Distinct there's no guarantee that items will come back in any particular order. In this case the order is very important so that values end up in the right columns.
So in your example above, even though there's a blank in the third column of the first row, the value from the fourth column ends up in the third column and the blank goes to the end.
That will likely fix the immediate problem. I'd recommend not using a List<string> when you're expecting a certain number of values (they need to match up with columns) because a List<string> can contain any number of values.
Instead, try something like this:
public class WhateverThisIs
{
public string Signature { get; set; }
public string HashValueSha1 { get; set; }
public string HashValueSha2 { get; set; }
public string HashValueMd5 { get; set; }
public string Other { get; set; }
}
Then, as a starting point, use Dictionary<string, WhateverThisIs>.
Then the part that outputs lines would look more like this:
var value = keyvaluepair.Value;
var values = String.Join(",", value.Signature, value.HashValueSha1, value.HashValueSha2,
value.HashValueMd5, value.Other);
(and yes, that accounts for null values.)
If you want to replace nulls or empty values with "N/A" then you'd need a separate function for that, like
string ReplaceNullWithNa(string value)
{
return string.IsNullOrEmpty(value) ? "N/A" : value;
}

creating array of bad names to check and replace in c#

I'm looking to create a method that loops through an list and replaces with matched values with a new value. I have something working below but it really doesnt follow the DRY principal and looks ugly.
How could I create a dictionary of value pairs that would hold my data of values to match and replace?
var match = acreData.data;
foreach(var i in match)
{
if (i.county_name == "DE KALB")
{
i.county_name = "DEKALB";
}
if (i.county_name == "DU PAGE")
{
i.county_name = "DUPAGE";
}
}
In your question, you can try to use linq and Replace to make it.
var match = acreData.data.ToList();
match.ForEach(x =>
x.county_name = x.county_name.Replace(" ", "")
);
or you can try to create a mapper table to let your data mapper with your value. as #user2864740 say.
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("DE KALB", "DEKALB");
dict.Add("DU PAGE", "DUPAGE");
var match = acreData.data;
string val = string.Empty;
foreach (var i in match)
{
if (dict.TryGetValue(i.county_name, out val))
i.county_name = val;
}
If this were my problem and it is possible a county could have more than one common misspelling I would create a class to hold the correct name and the common misspellings. The you could easily determine if the misspelling exists and correct if. Something like this:
public class County
{
public string CountyName { get; set; }
public List<string> CommonMisspellings { get; set; }
public County()
{
CommonMisspellings = new List<string>();
}
}
Usage:
//most likely populate from db
var counties = new List<County>();
var dekalb = new County { CountyName = "DEKALB" };
dekalb.CommonMisspellings.Add("DE KALB");
dekalb.CommonMisspellings.Add("DE_KALB");
var test = "DE KALB";
if (counties.Any(c => c.CommonMisspellings.Contains(test)))
{
test = counties.First(c => c.CommonMisspellings.Contains(test)).CountyName;
}
If you are simply replacing all words in a list containing space without space, then can use below:
var newList = match.ConvertAll(word => word.Replace(" ", ""));
ConvertAll returns a new list.
Also, I suggest not to use variable names like i, j, k etc..but use temp etc.
Sample code below:
var oldList = new List<string> {"DE KALB", "DE PAGE"};
var newList = oldList.ConvertAll(word => word.Replace(" ", ""));
We can try removing all the characters but letters and apostroph (Cote d'Ivoire has it)
...
i.country_name = String.Concat(i.country_name
.Where(c => char.IsLetter(c) || c == '\''));
...
I made a comment under answer of #Kevin and it seems it needs further explanation. Sequential searching in list does not scale well and unfortunately for Kevin, that is not my opinion, asymptotic computational complexity is math. While searching in dictionary is more or less O(1), searching in list is O(n). To show a practical impact for solution with 100 countries with 100 misspellings each, lets make a test
public class Country
{
public string CountryName { get; set; }
public List<string> CommonMisspellings { get; set; }
public Country()
{
CommonMisspellings = new List<string>();
}
}
static void Main()
{
var counties = new List<Country>();
Dictionary<string, string> dict = new Dictionary<string, string>();
Random rnd = new Random();
List<string> allCountryNames = new List<string>();
List<string> allMissNames = new List<string>();
for (int state = 0; state < 100; ++state)
{
string countryName = state.ToString() + rnd.NextDouble();
allCountryNames.Add(countryName);
var country = new Country { CountryName = countryName };
counties.Add(country);
for (int miss = 0; miss < 100; ++miss)
{
string missname = countryName + miss;
allMissNames.Add(missname);
country.CommonMisspellings.Add(missname);
dict.Add(missname, countryName);
}
}
List<string> testNames = new List<string>();
for (int i = 0; i < 100000; ++i)
{
if (rnd.Next(20) == 1)
{
testNames.Add(allMissNames[rnd.Next(allMissNames.Count)]);
}
else
{
testNames.Add(allCountryNames[rnd.Next(allCountryNames.Count)]);
}
}
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
st.Start();
List<string> repairs = new List<string>();
foreach (var test in testNames)
{
if (counties.Any(c => c.CommonMisspellings.Contains(test)))
{
repairs.Add(counties.First(c => c.CommonMisspellings.Contains(test)).CountryName);
}
}
st.Stop();
Console.WriteLine("List approach: " + st.ElapsedMilliseconds.ToString() + "ms");
st = new System.Diagnostics.Stopwatch();
st.Start();
List<string> repairsDict = new List<string>();
foreach (var test in testNames)
{
if (dict.TryGetValue(test, out var val))
{
repairsDict.Add(val);
}
}
st.Stop();
Console.WriteLine("Dict approach: " + st.ElapsedMilliseconds.ToString() + "ms");
Console.WriteLine("Repaired count: " + repairs.Count
+ ", check " + (repairs.SequenceEqual(repairsDict) ? "OK" : "ERROR"));
Console.ReadLine();
}
And the result is
List approach: 7264ms
Dict approach: 4ms
Repaired count: 4968, check OK
List approach is about 1800x slower, actually more the thousand times slower in this case. The results are as expected. If that is a problem is another question, it depends on concrete usage pattern in concrete application and is out of scope of this post.

String concatenation C# with List of Class

this may be very fundamental C# question as I only study C# myself there are things that I do not have the logic to start.
I have a class CustomerSite with string Customer {get;set} and string Site {get;set;}
I create a list List<CustomerSite> listCustomerSite= new List<CustomerSite>();
Assume, I have a list with the following data
SAMSUNG CHINA
SAMSUNG AMERICA
SAMSUNG AFRICA
LG CHINA
APPLE AMERICA
APPLE CHINA
I would like to have 1 concatenated string
string Result = "APPLE (AMERICA, CHINA), LG (CHINA), SAMSUNG (AFRICA, AMERICA, CHINA)"
How could I do that?
My idea is to use a dictionary to keep a list of distinct Customers and adding the site to the string but I still have no clue how to deal with sorting (AFRICA --> AMERICA --> CHINA)
Dictionary<string, int> dictCustomer = new Dictionary<string, int>();
foreach (var i in listCustomerSite)
{
if (!dictCustomer.ContainsKey(i.Customer))
{
dictCustomer.Add(i.Customer, 0);
Result = Result + "," + "i.Customer" + "( i.Site) ";
}
else
{
Result.Replace(")", "," + i.Site + ")");
}
}
You can utilize LINQ and String.Join to group your collection by customer, convert each grouping to a formatted string with sorted comma-separated sites, and then combine them to a single string:
var customersWithLocations = listCustomerSite
.GroupBy(cs => cs.Customer)
.Select(g => $"{g.Key} ({String.Join(", ", g.Select(cs => cs.Site).OrderBy(s => s))})")
.ToArray();
string result = String.Join(", ", customersWithLocations);
A one line LINQ statment can do it all:
var list = listCustomerSite
.GroupBy(c => c.Customer)
.Select(g => $"{g.Key} ({string.Join(", ", g.Select(c => c.Site))})")
.ToList();
This should produce the desired output, sorted as required:
public class CustomerSite
{
public CustomerSite(string customer, string site)
{
Customer = customer;
Site = site;
}
public string Customer { get; set; }
public string Site { get; set; }
}
class Program
{
static void Main(string[] args)
{
var listCustomerSite = new List<CustomerSite>()
{
new CustomerSite("SAMSUNG", "CHINA"),
new CustomerSite("SAMSUNG", "AMERICA"),
new CustomerSite("SAMSUNG", "AFRICA"),
new CustomerSite("LG", "CHINA"),
new CustomerSite("APPLE", "AMERICA"),
new CustomerSite("APPLE", "CHINA")
};
var list = from cs in listCustomerSite
group cs by cs.Customer into g
orderby g.Key
select $"{g.Key} ({string.Join(", ", g.OrderBy(c => c.Site).Select(c => c.Site))})";
Console.WriteLine(string.Join(", ", list));
Console.ReadKey();
}
}
Output:
APPLE (AMERICA, CHINA), LG (CHINA), SAMSUNG (AFRICA, AMERICA, CHINA)
Hope this helps!
The idea of using a dictionary is good; however, I would operate in two steps:
Gather the data into sorted collections.
Concatenate the customers and sites.
I'm not going to use LINQ, as it will be more instructive and show the fundamental way of doing things (since you are learning C#).
I am using a SortedDictionary<TKey, TValue> as it automatically sorts the entries by the key (the customers in our case).
var dictCustomer = new SortedDictionary<string, SortedSet<string>>();
The idea is to use the customer name as key and a sorted set of sites as value. A SortedSet<T> not only keeps the entries sorted, but also eliminates duplicates (which is not required here).
foreach (CustomerSite customerSite in listCustomerSite) {
if (dictCustomer.TryGetValue(customerSite.Customer, out var siteSet)) {
siteSet.Add(customerSite.Site);
} else {
siteSet = new SortedSet<string> { customerSite.Site };
dictCustomer.Add(customerSite.Customer, siteSet);
}
}
Now, let us build the result string:
var sb = new StringBuilder();
foreach (var keyValuePair in dictCustomer) {
if (sb.Length > 0) {
sb.Append(", ");
}
sb.Append(keyValuePair.Key).Append(" ("); // Appends the customer and " (".
bool nextSite = false;
foreach (string site in keyValuePair.Value) {
if (nextSite) {
sb.Append(", ");
}
sb.Append(site);
nextSite = true;
}
sb.Append(")");
}
string result = sb.ToString();
Note that a StringBuilder is more efficient than concatenating strings directly, since a StringBuilder uses a buffer internally which grows only as needed, while string concatenation creates a new string object at each step and involves copying an ever-growing string over and over.

Is there a way to include null values in a list in C#?

I have a text file filled with several lines of data, and I would like to split it into 5 different elements like so..
I am successfully reading in the data and putting it into an array. Now I would like to split each part of the text up into different lists so I can compare the data against one another.
I have currently managed to read in the first 4 elements of each line into their relevant lists but the 5th one is throwing me the error "System.IndexOutOfRangeException" which I can only assume is because the first line it reads in has no value for the 5th element?
So my question is, is there a way to populate null values when writing them to a number of lists?
I've tried manually assigning the size of the array and lists but I still get the same error.
Here is my code:
class Program
{
static void Main(string[] args)
{
// Reading in file containing data from BT Code Evaluation sheet (for testing purposes).
// Each line gets stored into a string array, each element is one line of the data.txt file.
//String[] lines = System.IO.File.ReadAllLines(#"C:\Users\Ad\Desktop\data.txt");
String[] lines = new String[5] {"monitorTime", "localTime", "actor", "action", "actor2"};
lines = System.IO.File.ReadAllLines(#"C:\Users\Ad\Desktop\data.txt");
char delimiter = ' ';
List<String> monitorTime = new List<String>();
List<String> localTime = new List<String>();
List<String> actor = new List<String>();
List<String> action = new List<String>();
List<String> actor2 = new List<String>();
// Foreach loop displays the lines of text in the data file.
foreach (String line in lines)
{
// Writes the data to the console.
Console.WriteLine(line);
String[] data = new String[5] { "monitorTime", "localTime", "actor", "action", "actor2" };
data = line.Split(delimiter);
monitorTime.Add(data[0]);
localTime.Add(data[1]);
actor.Add(data[2]);
action.Add(data[3]);
actor2.Add(data[4]);
}
foreach (String time in monitorTime)
{
Console.WriteLine(time);
}
foreach (String time in localTime)
{
Console.WriteLine(time);
}
foreach (String name in actor)
{
Console.WriteLine(name);
}
foreach (String actions in action)
{
Console.WriteLine(actions);
}
foreach (String name in actor2)
{
if (name != null)
{
Console.WriteLine("UNKNOWN");
}
else
{
Console.WriteLine(actor2);
}
}
// Creates an empty line between the data and the following text.
Console.WriteLine("");
// Displays message in console.
Console.WriteLine("Press any key to analyse data and create report...");
Console.ReadKey();
}
}
You need to check the bounds of you array before you try to add. If their aren't enough items you can add null instead.
For example:
actor2.Add(data.length > 4 ? data[4] : null)
(Note: You could do the same type of check on the other items as well, unless you are positive that the last item is the only one that might be null)
This is using the ternary operator, but you could also use a simple if/else, but it'll be more verbose. It's equivalent to:
if (data.length > 4)
{
actor2.Add(data[4]);
}
else
{
actor2.Add(null);
}
This along with Console.WriteLine(name); instead of Console.WriteLine(actor2); should fix you immediate problem.
However, a much better design here would be to have a single list of objects with MonitorTime, LocalTime, Actor, Action and Actor2 properties. That way you don't ever have to worry that the 5 parallel arrays might get out of sync.
For example, create a class like this:
public class DataItem
{
public string MonitorTime { get; set; }
public string LocalTime { get; set; }
public string Actor { get; set; }
public string Action { get; set; }
public string Actor2 { get; set; }
}
Then instead of your 5 List<String>, you have one List<DataItem>:
List<DataItem> dataList = new List<DataItem>();
Then in your loop to populate it you'd do something like:
data = line.Split(delimiter);
dataList.Add(new DataItem()
{
MonitorTime = data[0],
LocalTime = data[1],
Actor = data[2],
Action = data[3],
Actor2 = data.length > 4 ? data[4] : null
});
Then you can access them later with something like:
foreach (var item in dataList)
{
Console.WriteLine(item.MonitorTime);
//...
}
In your for each you should be checking to see if the index exists before populating the object.
foreach (String line in lines)
{
// Writes the data to the console.
Console.WriteLine(line);
String[] data = new String[5] { "monitorTime", "localTime", "actor", "action", "actor2" };
data = line.Split(delimiter);
monitorTime.Add(data[0]);
localTime.Add(data[1]);
actor.Add(data[2]);
action.Add(data[3]);
if (data.Length > 4) {
actor2.Add(data[4]);
}
}
There's better ways to do this but this is a simple solution for now.

What is the easiest way to split columns from a txt file

I've been looking around a bit but haven't really found a good example with what I'm struggling right now.
I have a .txt file with a couple of columns as follows:
# ID,YYYYMMDD, COLD,WATER, OD, OP,
52,20120406, 112, 91, 20, 130,
53,20130601, 332, 11, 33, 120,
And I'm reading these from the file into a string[] array.
I'd like to split them into a list
for example
List results, and [0] index will be the first index of the columns
results[0].ID
results[0].COLD
etc..
Now I've been looking around, and came up with the "\\\s+" split
but I'm not sure how to go about it since each entry is under another one.
string[] lines = File.ReadAllLines(path);
List<Bus> results = new List<Bus>();
//Bus = class with all the vars in it
//such as Bus.ID, Bus.COLD, Bus.YYYYMMDD
foreach (line in lines) {
var val = line.Split("\\s+");
//not sure where to go from here
}
Would greatly appreciate any help!
Kind regards, Venomous.
I suggest using Linq, something like this:
List<Bus> results = File
.ReadLines(#"C:\MyFile.txt") // we have no need to read All lines in one go
.Skip(1) // skip file's title
.Select(line => line.Split(','))
.Select(items => new Bus( //TODO: check constructor's syntax
int.Parse(items[1]),
int.Parse(items[3]),
DateTime.ParseExact(items[2], "yyyyMMdd", CultureInfo.InvariantCulture)))
.ToList();
I would do
public class Foo
{
public int Id {get; set;}
public string Date {get; set;}
public double Cold {get; set;}
//...more
}
Then read the file
var l = new List<Foo>();
foreach (line in lines)
{
var sp = line.Split(',');
var foo = new Foo
{
Id = int.Parse(sp[0].Trim()),
Date = sp[1].Trim(),//or pharse the date to a date time struct
Cold = double.Parse(sp[2].Trim())
}
l.Add(foo);
}
//now l contains a list filled with Foo objects
I would probably keep a List of properties and use reflection to populate the object, something like this :
var columnMap = new[]{"ID","YYYYMMDD","COLD","WATER","OD","OP"};
var properties = columnMap.Select(typeof(Bus).GetProperty).ToList();
var resultList = new List<Bus>();
foreach(var line in lines)
{
var val = line.Split(',');
var adding = new Bus();
for(int i=0;i<val.Length;i++)
{
properties.ForEach(p=>p.SetValue(adding,val[i]));
}
resultList.Add(adding);
}
This is assuming that all of your properties are strings however
Something like this perhaps...
results.Add(new Bus
{
ID = val[0],
YYYYMMDD = val[1],
COLD = val[2],
WATER = val[3],
OD = val[4],
OP = val[5]
});
Keep in mind that all of the values in the val array are still strings at this point. If the properties of Bus are typed, you will need to parse them into the correct types e.g. assume ID is typed as an int...
ID = string.IsNullOrEmpty(val[0]) ? default(int) : int.Parse(val[0]),
Also, if the column headers are actually present in the file in the first line, you'll need to skip/disregard that line and process the rest.
Given that we have the Bus class with all the variables from your textfile:
class Bus
{
public int id;
public DateTime date;
public int cold;
public int water;
public int od;
public int op;
public Bus(int _id, DateTime _date, int _cold, int _water, int _od, int _op)
{
id = _id;
date = _date;
cold = _cold;
water = _water;
od = _od;
op = _op;
}
}
Then we can list them all in the results list like this:
List<Bus> results = new List<Bus>();
foreach (string line in File.ReadAllLines(path))
{
if (line.StartsWith("#"))
continue;
string[] parts = line.Replace(" ", "").Split(','); // Remove all spaces and split at commas
results.Add(new Bus(
int.Parse(parts[0]),
DateTime.ParseExact(parts[1], "yyyyMMdd", CultureInfo.InvariantCulture),
int.Parse(parts[2]),
int.Parse(parts[3]),
int.Parse(parts[4]),
int.Parse(parts[5])
));
}
And access the values as you wish:
results[0].id;
results[0].cold;
//etc.
I hope this helps.

Categories