3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C# のListからDictionaryへの変換

Last updated at Posted at 2025-01-30

やり方

LINQ のToDictionaryメソッドを使う。

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var pointCards = new List<PointCard>(){
                new PointCard(){UserID=1,CardName="Aカード",Point=10, },
                new PointCard(){UserID=2,CardName="Bカード",Point =15, },
                new PointCard(){UserID=3,CardName="Cカード",Point =20, },
            };

            // ListからDictionaryに変換
            //  引数が1つならKeyを指定
            //  引数が2つならKeyとValueを指定
            Dictionary<int, PointCard> pointCardDict
                = pointCards.ToDictionary(x => x.UserID);

            // ↑と同じ
            Dictionary<int, PointCard> pointCardDict2
                = pointCards.ToDictionary(x => x.UserID, x => x);
        }
    }

    public class PointCard
    {
        public int UserID { get; set; }
        public string CardName { get; set; }
        public int Point { get; set; }
    }
}

顧客管理とか、1人のユーザーに対して複数あるものを扱う時に使えそう。

GroupByを組み合わせる

@junerさんからコメントいただきましたので追記します。
ありがとうございます!(勉強になります(^^♪)

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var pointCards = new List<PointCard>(){
                new PointCard(){UserID=1,CardName="Aカード",Point=10, },
                new PointCard(){UserID=2,CardName="Bカード",Point =15, },
                new PointCard(){UserID=3,CardName="Cカード",Point =20, },
                new PointCard(){UserID=1,CardName="Dカード",Point =10, },
                new PointCard(){UserID=1,CardName="Eカード",Point =25, },
                new PointCard(){UserID=2,CardName="Fカード",Point =30, },
            };

            // ユーザー毎にまとめてみた
            Dictionary<int, IGrouping<int, PointCard>> pointCardDictEachUser =
                pointCards.GroupBy(x => x.UserID).ToDictionary(x => x.Key);

            foreach (KeyValuePair<int, IGrouping<int, PointCard>> pair in pointCardDictEachUser)
            {
                Console.WriteLine(pair.Key);
                foreach (var cards in pair.Value)
                {
                    Console.WriteLine(cards.CardName);
                }
            }

            Console.ReadKey();
        }
    }

    public class PointCard
    {
        public int UserID { get; set; }
        public string CardName { get; set; }
        public int Point { get; set; }
    }
}

3
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?