有效地向列表中添加项目并行c#

本文关键字:class 并行 项目 列表 添加 有效地 | 更新日期: 2023-09-27 18:05:38

我有功能的代码,分割类列表中的一个属性的字符串:由string, string, string组成的Dataframe。

现在我正在声明一个空的Dataframe2 (string,string[], string),并使用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});
        } );
    }
}

是否有一种更有效的方法来添加的东西,使用并行LINQ其中Dataframe2继承属性我没有修改列表?

有效地向列表中添加项目<class>并行c#

您可以尝试使用PLinq来添加并行性并保留List<T>:

// Do NOT create and then fill the List<T> (which is not thread-safe) in parallel manually,
// Let PLinq do it for you
List<Dataframe2> doc2 = doc
  .AsParallel()
  .Select(entry => {
     //TODO: make Dataframe2 from given Dataframe (entry)
     ...
     return new Dataframe2 {Name = entry.Name, Text = splitter, Cat = entry.Cat};
  }) 
  .ToList();