I have functioning code that splits the strings of one property in a list of class: Dataframe made of string, string, string
.
Right now I am declaring an empty Dataframe2 (string,string[], string
) and appending items to the list using Add
class Program
{
public static string[] SPString(string text)
{
string[] elements;
elements = text.Split(' ');
return elements;
}
//Structures
public class Dataframe
{
public string Name { get; set; }
public string Text { get; set; }
public string Cat { get; set; }
}
public class Dataframe2
{
public string Name { get; set; }
public string[] Text { get; set; }
public string Cat { get; set; }
}
static void Main(string[] args)
{
List<Dataframe> doc = new List<Dataframe>{new Dataframe { Name = "Doc1", Text = "The quick brown cat", Cat = ""},
new Dataframe { Name = "Doc2", Text = "The big fat cat", Cat = "Two"},
new Dataframe { Name = "Doc4", Text = "The quick brown rat", Cat = "One"},
new Dataframe { Name = "Doc3", Text = "Its the cat in the hat", Cat = "Two"},
new Dataframe { Name = "Doc5", Text = "Mice and rats eat seeds", Cat = "One"},
};
// Can this be made more efficient?
ConcurrentBag<Dataframe2> doc2 = new ConcurrentBag<Dataframe2>();
Parallel.ForEach(doc, entry =>
{
string s = entry.Text;
string[] splitter = SPString(s);
doc2.Add(new Dataframe2 {Name = entry.Name, Text = splitter, Cat =entry.Cat});
} );
}
}
Is there a more efficient way to add stuff to a list using a parallel LINQ where Dataframe2 inherits the properties I did not modify?