iT邦幫忙

0

C# LIST 合併

我有好幾份Excel檔案,因為要的欄位都不同,所以我都先各別用LIST把需要的存下來
然後有一份要先把名稱一樣的篩選過,後面的值做相加,我是用Groupby處理完,可是我想把其他份的LIST做合併,卻不能。

    public class user
    {
        public string item { get; set; }
        public decimal totalRec { get; set; }
        public decimal MTDA { get; set; }
        public decimal MTDOrder { get; set; }
        public decimal current { get; set; }
        public decimal next { get; set; }
        public decimal after { get; set; }

    }

 using (ExcelPackage ep3 = new ExcelPackage(new   System.IO.FileInfo(@"C:\Users\10912048\Desktop\WKSTRST\WCQ FCST control table202105.xlsx")))
                {
                    ExcelWorksheet sh3 = ep3.Workbook.Worksheets["Summary"];

                    var WCDme1 = new List<user>();
                    do
                    {
                        int WCDcells = 2;
                        try
                        {
                            for (int r = 3; r <= sh3.Dimension.Rows; r++)
                            {
                                WCDme1.Add(new user
                                {
                                    item = sh1.Cells[r, 1].Value?.ToString(),
                                    totalRec = decimal.Parse(sh1.Cells[r, 33].Value?.ToString() ?? "0"),
                                    MTDA = decimal.Parse(sh1.Cells[r, 57].Value?.ToString() ?? "0"),
                                    MTDOrder = decimal.Parse(sh1.Cells[r, 56].Value?.ToString() ?? "0"),
                                    current = decimal.Parse(sh1.Cells[r, 59].Value?.ToString() ?? "0"),
                                    next = decimal.Parse(sh1.Cells[r, 61].Value?.ToString() ?? "0"),
                                    after = decimal.Parse(sh1.Cells[r, 63].Value?.ToString() ?? "0"),
                                });
                                WCDcells++;
                                error = false;
                            }
                        }
                        catch
                        {
                            sh3.DeleteRow(WCDcells);
                            Console.WriteLine("WCQ Row" + WCDcells + " error");
                            error = true;
                            WCDme1.Clear();
                        }
                    } while (error == true);

                    var WCDme = from a in WCDme1
                                group a by new { a.item} into av
                                select new
                                {
                                    av.Key.item,
                                    MTDA = av.Sum(x => x.totalRec) + av.Sum(x => x.MTDA),
                                    MTDOrder = av.Sum(x => x.MTDOrder),
                                    current = av.Sum(x => x.current),
                                    next = av.Sum(x => x.next),
                                    after = av.Sum(x => x.after),

                                };
                    //將list合併
                    var ALLlist = WCDme.Union(WCQme).ToList();

                }
                
          問題出在這一行
 var ALLlist = WCDme.Union(WCQme).ToList();

說明需要ParallelQuery的接收器。

https://ithelp.ithome.com.tw/upload/images/20210521/20136928XmCGPnmMkw.png

我有三個List,只有一個有先用Groupby做過處裡(只有一份需要),然後我想將3個變成一個LIST就好了,不需要篩選,算是只有合併而已。

1 個回答

2
japhenchen
iT邦大師 1 級 ‧ 2021-05-21 12:37:01
最佳解答
List<string> alist = new List<string>();
List<string> blist = new List<string>();

//a跟b兩兄弟各自爬山.......抓資料
// ..to do get lists.....................................
//

alist.AddRange(blist);  //合併完了

再複雜的list都能合併,只要兩個list都是同一個class <T>

我要發表回答

立即登入回答