0

集計カウントを使用して月ごとにグループ化することで、Linq に関する問題を解決する方法を知りたいです。

以下は私のサンプルデータです:

Code     CodeName   Color       Month
11111     One       Red          1
11111     One       Red          1
11111     One       Red          2
22222     Two       Green        2
33333     Three     Yellow       3
44444     Four      Blue         4
44444     Four      Blue         4
55555     Five      White        5

私はこのような結果を見たい:

Code      CodeName     Color     Count(Mont1)   Month2     Month3    Month4    Month5
11111       one        red           2            1          0         0         0
22222       two        Green         0            1          0         0         0
33333       three      Yellow        0            0          1         0         0
44444       four       Blue          0            0          0         2         0
55555       five       White         0            0          0         0         1
4

1 に答える 1

0

希望する結果が得られる例を次に示します

その核心はこれです:

  • Codeプロパティごとに要素をグループ化する
  • プロパティごとに各グループ内の要素をグループ化しColorます
  • 各グループを処理し、各MonthXプロパティが指定された月の識別子を持つ内側のグループ (コードごとの色ごとのアイテム) 内のオブジェクトの数に設定された結果を作成します

これは、質問で提供された 5 か月の値を具体的に処理します。必要なすべての月の値を結果オブジェクトの独自のプロパティに分割するか、他の値が必要な場合はそれを月のインデックスとカウントの辞書に構築できます。

public enum Number
{
    One = 11111,
    Two = 22222,
    Three = 33333,
    Four = 44444,
    Five = 55555
}

public class Data
{
    public Number Code { get; set; }
    public string CodeName { get { return Enum.GetName(typeof(Number), Code); } }
    public ConsoleColor Color { get; set; }
    public int Month { get; set; }
}

public class Result
{
    public Number Code { get; set; }
    public string CodeName { get { return Enum.GetName(typeof(Number), Code); } }
    public ConsoleColor Color { get; set; }
    public int Month1 { get; set; }
    public int Month2 { get; set; }
    public int Month3 { get; set; }
    public int Month4 { get; set; }
    public int Month5 { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var items = new Data[]
        {
            new Data{Code = Number.One, Color = ConsoleColor.Red, Month = 1},
            new Data{Code = Number.One, Color = ConsoleColor.Red, Month = 1},
            new Data{Code = Number.One, Color = ConsoleColor.Red, Month = 2},
            new Data{Code = Number.Two, Color = ConsoleColor.Green, Month = 2},
            new Data{Code = Number.Three, Color = ConsoleColor.Yellow, Month = 3},
            new Data{Code = Number.Four, Color = ConsoleColor.Blue, Month = 4},
            new Data{Code = Number.Four, Color = ConsoleColor.Blue, Month = 4},
            new Data{Code = Number.Five, Color = ConsoleColor.White, Month = 5},
        };

        var results = items.GroupBy(x => x.Code).Select(
            x => x.GroupBy(y => y.Color)
                  .Select(z => new Result
                  {
                      Code = x.Key,
                      Color = z.Key,
                      Month1 = z.Count(q => q.Month == 1),
                      Month2 = z.Count(q => q.Month == 2),
                      Month3 = z.Count(q => q.Month == 3),
                      Month4 = z.Count(q => q.Month == 4),
                      Month5 = z.Count(q => q.Month == 5),
                  }).ToList());

        var resultList = results.ToList();
    }
}
于 2013-06-26T20:01:55.367 に答える