C#.NET: List<Source>轉Dictionary<Source.Key,List<Source>>

來源:互聯網
上載者:User

C#.NET 利用ToDictionary(),GroupBy(),可以將List轉化為Dictionary,主需要一行代碼!

首先看一下需求,已知cars,等於:

                List<Car> cars = new List<Car>(){                                new Car(1,"audiA6","private"),                                new Car(2,"futon","merchant"),                                new Car(3,"feiren","bike"),                                new Car(4,"bukon","private"),                                new Car(5,"baoma","private"),                                new Car(6,"dayun","merchant")            };

1)我想以id為鍵,值為Car轉化為一個字典idCarDict,方法如下:

var idCarDict = cars.ToDictionary(car=>car.id);

這樣保證能正確轉化的前提為,id在列表中沒有重複值。如果有重複的,會拋出向字典中添加重複值的異常。

2)我想以type為鍵,值car的List的typeDict,方法如下:

Dictionary<string, List<Car>> typeCarDict = cars.GroupBy(car => car.type).ToDictionary(r => r.Key, r => r.ToList());

分步解釋:
第一步分組

cars.GroupBy(car=>car.type) //返回的結果類型為: //IEnumerable<IGroup<string,car>>;//其中string等於car.type,也就是分組的鍵

第二步將IEnumerable類型轉化為字典,選取合適的鍵,

ToDictionary(r=>r.Key,r=>r.ToList());//r參數代表分組對象,r.Key便是car.type;//r.ToList()操作後將分組對象轉化為List對象

這種轉化代碼簡介,比以下foreach遍曆得到以car.type的字典簡潔許多:

var dict = new Dictionary<string,List<Car>>();foreach(var car in cars){  if(dict.Contains(car.type))     dict[car.type].Add(car);  else    dict.Add(car.type,new List<Car>(){car}));}

C#.NET 利用ToDictionary(),GroupBy(),可以將List轉化為Dictionary,主需要一行代碼!

首先看一下需求,已知cars,等於:

         List<Car> cars = new List<Car>(){                new Car(1,"audiA6","private"),                new Car(2,"futon","merchant"),                new Car(3,"feiren","bike"),                new Car(4,"bukon","private"),                new Car(5,"baoma","private"),                new Car(6,"dayun","merchant")            };

1)我想以id為鍵,值為Car轉化為一個字典idCarDict,方法如下:

var idCarDict = cars.ToDictionary(car=>car.id);

這樣保證能正確轉化的前提為,id在列表中沒有重複值。如果有重複的,會拋出向字典中添加重複值的異常。

2)我想以type為鍵,值car的List的typeDict,方法如下:

Dictionary<string, List<Car>> typeCarDict = cars.GroupBy(car => car.type).ToDictionary(r => r.Key, r => r.ToList());

分步解釋:
第一步分組

cars.GroupBy(car=>car.type) //返回的結果類型為: //IEnumerable<IGroup<string,car>>;//其中string等於car.type,也就是分組的鍵

第二步將IEnumerable類型轉化為字典,選取合適的鍵,

ToDictionary(r=>r.Key,r=>r.ToList());//r參數代表分組對象,r.Key便是car.type;//r.ToList()操作後將分組對象轉化為List對象

這種轉化代碼簡介,比以下foreach遍曆得到以car.type的字典簡潔許多:

var dict = new Dictionary<string,List<Car>>();foreach(var car in cars){  if(dict.Contains(car.type))     dict[car.type].Add(car);  else    dict.Add(car.type,new List<Car>(){car}));}
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.