0

例を挙げて説明しましょう。

var vProducts = new[] {
    new { Product = "A", Location ="Y", Month = "January", Demand = 50 },
    new { Product = "A", Location ="Y", Month = "February", Demand = 100 },
    new { Product = "A", Location ="Y", Month = "March", Demand = 20 },
    new { Product = "A", Location ="Y", Month = "June", Demand = 10 }
};

var vPeriods = new[] {
    new { Priority = 1, Month = "January" },
    new { Priority  = 2, Month = "February" },
    new { Priority  = 3, Month = "March" },
    new { Priority  = 4, Month = "April" },
    new { Priority  = 5, Month = "May" },
    new { Priority  = 6, Month = "June" }
};

var vAll = from p in vProducts
       from t in vPeriods
       select new
           {
             Product = p.Product,
             Location = p.Location,
             Period = t.Priority,
             PeriodName = t.Month,
             Demand = p.Demand
           };

上記のクエリは、製品と期間のすべての組み合わせを作成します。ただし、以下に示すように、月が一致しない製品と一緒にすべての製品のリストを取得する必要があります。

Product      Location      Priority        Month    Demand
  A              Y            1           January     50
  A              Y            2          February    100
  A              Y            3           March       20
  A              Y            4           April       null
  A              Y            5           May         null
  A              Y            6           June        10

コメントありがとうございます。

4

1 に答える 1

0

左外側の結合を実行したい場合は、次のようになります。

var res = from period in vPeriods
                  join product in vProducts on period.Month equals product.Month into bla
                  from p in bla.DefaultIfEmpty()
                  select new { period.Month, period.Priority, Product = p == null ? null : p.Product, Demand = p == null ? -1 : p.Demand };

  foreach (var a in res)
            {
                Console.WriteLine(string.Format("{0} {1} {2}", a.Product, a.Month, a.Demand));
            }

もちろん、一致する月がない製品には場所などがありません(例で述べたように)

于 2012-08-08T18:39:14.600 に答える