Title: C# object to list c#
Tags: c#;arraylist;listobject
Question: I am reading in a csv file that I would like to add the second column into the same list as a column that match the name. I check the the next line is equal to the previous record but then I loop the array finding the match but I am not sure how to add that  internalList back into the class object.
is there a better way to do this?
Program
while ((s = sr.ReadLine()) != null)
string[] words = s.Split('\t');
if (previousrecord == words[0])
for (int i = 0; i < ALEComName.Count; ++i)
{
}
Name person = new Name();
person.Name = words[0];
List<SubName&gt; internalList = new List<SubName&gt;();
SubName AssociatedSub = new SubName { Name = words[1] };
internalList.Add(AssociatedSub);
person.AssociatedSub = internalList;
ALEComName.Add(Disease);
previousrecord = words[0];
public class Name
public string Name { get; set; }
public List<SubName&gt; AssociatedSub { get; set; }
public class SubName
public string Name { get; set; }
CSV File

Here is the accepted answer: You can read all lines, then use Linq:
var data = File.ReadAllLines(@&quot;c:\temp\sample.txt&quot;);
var names = data.Select(d =&gt; d.Split('\t'))
.Select(s =&gt; new { Name = s[0], SubName = s[1] })
.GroupBy(o =&gt; o.Name)
.Select(g =&gt; new Name()
Name1 = g.Key,
AssociatedSub = g.Select(v =&gt; new SubName() { Name = v.SubName }).ToList()
//This part is just to show the output
foreach (var name in names)
Console.WriteLine($&quot;Name: {name.Name1}, AssociatedSub: {string.Join(&quot;,&quot;, name.AssociatedSub.Select(s =&gt; s.Name).ToArray())}&quot;);
Output:
Name: A, AssociatedSub: A
Name: B, AssociatedSub: B
Name: C, AssociatedSub: A,B,C
Name: D, AssociatedSub: A,B
I had to change the name of the property to Name1 since it's an invalid language construct.
You first select the result of the split, then create an anonymous type with Name and SubName properties that will be used for grouping. Lastly, you select from the grouped results and create the instances.
This is just a quick sample so be careful for errors such as the Split not returning the expected numbers of parts.

Here is another answer: The Linq approach is very nice, I agree with the idea. I made a dictionary approach in case you wanted a more conservative way, which stores key,value pairs.
class Program {
static void Main(string[] args) {
using(var file = new StreamReader(@"../file.csv")) {
var dict = new Dictionary<string, List<string&gt;&gt;();
List<string&gt; split;
string line, key;
while((line = file.ReadLine()) != null) {
split = line.Select(l =&gt; new string(l, 1)).Where(l =&gt; l != " ").ToList();
key   = split[0];
split.RemoveAt(0);
if(dict.ContainsKey(key)) {
dict.TryGetValue(key, out var values);
values.AddRange(split);
} else dict.Add(key, split);
}
foreach(KeyValuePair<string, List<string&gt;&gt; r in dict) {
foreach(var val in r.Value) {
Console.WriteLine("Main = {0}, Sub = {1}", r.Key, val);
}
}
}
}

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:11/09/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><arraylist><listobject>', 'Title': 'C# object to list c#', 'ViewCount': 119, 'id': 53232511, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}