簡體   English   中英

來自 C# 中大型 CSV 文件的 WPF Datagrid

[英]WPF Datagrid from large CSV file in C#

我正在嘗試將 CSV 導入List<Model>()

我的 CSV 有超過 30,000 行/行的數據。 我發現將它加載到模型列表中需要很長時間,然后綁定到datagrid

我試過使用CsvHelperFile.ReadAllLines(file).ToList(); ,兩者都需要很長時間才能加載。

關於加快 CSV 數據導入的任何建議?

我猜可能不是讀取文件需要這么長時間。 可能是我將每一行存儲為一個類嗎? 我猜這都存儲在內存中,所有 30,000 個條目。 有沒有更好的方法來填充 DataGrid?

代碼:

        // PersonDataView
    <DataGrid X:Name="Employees" > 

    </DataGrid>

    public class PersonModel
    {
        /// <summary>
        /// The unique identifier for the person.
        /// </summary>
        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string EmailAddress { get; set; }

        public string CellphoneNumber { get; set; }

        public string Other { get; set; }
    }

    public class PersonDataViewModel : Screen
    {

      public List<PersonModel> _employees = new List<PersonModel>();
      public List<PersonModel> Employees
      {
        get { return _employees; }
        set { 
          _employees = value;
          NotifyOfPropertyChange(() => EmployeeData)
        }
      }

      public PersonDataViewModel()
      {
        Employees = new BindableCollection<PersonModel>(GetPersonData_All());
      }
    }


    public static BindableCollection<PrizeModel>     GetPersonData_All(this List<string> lines)
    {
        List<PrizeModel> output = new List<PrizeModel>();
        string file = "EmployeeModel.csv";

        var lines = File.ReadAllLines(filepath);

        foreach (string line in lines)
        {
            string[] cols = line.Split(',');

            PersonModel p = new PersonModel();
            p.Id = int.Parse(cols[0]);
            p.FirstName = int.Parse(cols[1]);
            p.LastName = cols[2];
            p.EmailAddress = decimal.Parse(cols[3]);
            p.CellphoneNumber = double.Parse(cols[4]);
            p.Other = double.Parse(cols[4]);

            output.Add(p);
        }

        return output;
    }

我不知道你是否總是有問題,但我分享我的經驗。 在讀取文件時很難贏得時間,除非您將文件分成更多部分並使用多線程讀取不同部分。

你可以贏得大量時間的地方是閱讀和處理而不是我閱讀所有文件然后我處理,我讀一行然后我處理一行..

為了幫助做到這一點,您可以使用技術昵稱“ Blocking Collection ”示例

如果您可以將文件分成多個部分,那就更好了

void ReadAndProcessFiles(string[] filePaths)
{
    // Our thread-safe collection used for the handover.
    var lines = new BlockingCollection<string>();

    // Build the pipeline.
    var phase1 = Task.Run(() =>
    {
        try
        {
            foreach (var filePath in filePaths)
            {
                using (var reader = new StreamReader(filePath))
                {
                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        // Hand over to phase 2 and continue reading.
                        lines.Add(line);
                    }
                }
            }
        }
        finally
        {
            lines.CompleteAdding();
        }
    });

    var phase2 = Task.Run(() =>
    {
        // Process lines on a ThreadPool thread
        // as soon as they become available.
        foreach (var line in lines.GetConsumingEnumerable())
        {
            //DO something during phase2
        }
    });

    // Block until both tasks have completed.
    // This makes this method prone to deadlocking.
    // Consider using 'await Task.WhenAll' instead.
    Task.WaitAll(phase1, phase2);
}

如果你可以鏈接更多的階段,那會越來越好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM