0

假设我有一个具有以下属性的对象。

    public string VendorNumber { get; set; }
    public string PartNumber { get; set; }
    public string PartDivision { get; set; }

我有一个大约 300 个这些对象的列表。我试图先按 VendorNumber 对它们进行分组,然后按 Division 对它们进行分组。之后我应该能够像这样钻入它们:

  1. 供应商 A
    第 1
    部分 -- 第 0001
    部分 -- 第 0002
    部分 -- 第 0003
    部分 第 2
    部分 -- 第 0001 部分
  2. Vendor B
    Division 1
    --Part 0023 ...等...

我可以像这样轻松地进行第一个分组:

var vendorGroups =
    from v in vendors
    group v by v.VendorNumber into vg
    select new { VendorNumber = vg.Key, Parts = vg, Count = vg.Count() };

即使参考了此处和 MSDN 上的其他一些帖子,我似乎也无法使嵌套分组工作。谢谢。


以下是我从建议中得出的结论:

我使用建议取得了一些进展,但循环时似乎无法访问第二级:

var vendorGroups = forecastHelpers
    .GroupBy(x => new { VendorNumber = x.VendorNumber, Division = x.PartDivision, Level = 1 })
    .GroupBy(x => new { VendorNumber = x.Key.VendorNumber }).OrderBy(x => x.Key.VendorNumber);

    foreach (var vendorGroup in vendorGroups)
{
    System.Diagnostics.Debug.WriteLine("VendorNumber: " + vendorGroup.Key.VendorNumber);
    foreach (var divisionGroup in vendorGroup)
    {
        System.Diagnostics.Debug.WriteLine("     Division: " + divisionGroup.Key.Division);
        foreach (var partNumber in divisionGroup)
        {
            System.Diagnostics.Debug.WriteLine("          PartNumber: " + partNumber.PartNumber);
        }
    }
}

更新 2:这也可以这样写:

var vendorGroupings = from f in forecastHelpers
                        group f by new { VendorNumber = f.VendorNumber, Division = f.PartDivision, Level = 2 } into vendorGroups
                        from divisionGroups in
                            (from division in vendorGroups
                            orderby division.PartDivision
                            group division by new { Division = division.PartDivision }) 
                        orderby vendorGroups.Key.VendorNumber
                        group divisionGroups by new { VendorNumber = vendorGroups.Key.VendorNumber, Level = 1 };


foreach (var vendorGroup in vendorGroupings)
{
    System.Diagnostics.Debug.WriteLine("VendorNumber: " + vendorGroup.Key.VendorNumber);

    foreach (var division in vendorGroup)
    {
        System.Diagnostics.Debug.WriteLine("     Division: " + division.Key.Division);

        foreach (var part in division)
        {
            System.Diagnostics.Debug.WriteLine("          PartNumber: " + part.PartNumber);
        }
    }
4

4 回答 4

1

尝试这个:

var vendorGroups = vendors.GroupBy(x => new { x.VendorNumber, x.PartDivision})
                          .GroupBy(x => x.Key.VendorNumber);

要访问第二级,您需要做x.Key.PartDivision,而不仅仅是x.Key(适用于顶级)。


要解决您的编辑:

var vendorGroups = forecastHelpers
    .GroupBy(x => new { VendorNumber = x.VendorNumber, Division = x.PartDivision, Level = 1 })
    .GroupBy(x => new { VendorNumber = x.Key.VendorNumber }).OrderBy(x => x.Key.VendorNumber);

foreach (var vendorGroup in vendorGroups)
{
    System.Diagnostics.Debug.WriteLine("VendorNumber: " + vendorGroup.Key.VendorNumber);  

    foreach(var division in vendorGroup)
    {
       System.Diagnostics.Debug.WriteLine("Division: " + division.Key.division); 
       foreach (var part in division)
       {
           System.Diagnostics.Debug.WriteLine("Part: " + part.PartNumber); 
       } 
    }
}
于 2013-10-08T13:31:18.047 回答
1
var grouped = from v in vendors
              group v by new { VN = v.VendorNumber, VD = v.PartDivision} into grp
              orderby grp.Key.VN, grp.Key.VD
              select new {VN_VD=grp.Key, Items=grp};
于 2013-10-08T13:18:57.310 回答
0
var vendorGroups =
    from v in vendors
    group v by v.VendorNumber into vg
    select new { VendorNumber = vg.Key, Parts = vg, Count = vg.Count() };
foreach (var vg in vendorGroups)
{
    var vendor = vg.VendorNumber;
    foreach (var divGroup in vg.Parts.GroupBy(x => x.PartDivision))
    {
        var division = divGroup.Key;
        foreach (var part in divGroup)
        {
            // do stuff with vendor, division, and part
        }
    }
}

它可能可以使用纯查询语法来完成,但我发现它按原样清晰简单。

于 2013-10-08T13:13:39.553 回答
0
var query = from x in set
            group x by x.VendorNumber into g
            select new 
            {
                VendorNumber = g.Key
                GroupedBy = g.ToLookup(x => x.PartDivision)                                 
            };

foreach(var x in query)
{
   var venderNumber = x.VendorNumber;
   foreach(var y in x.GroupBy)
   {
       var partDivision = y.Key
       foreach(Vendor item in y)
       {
           //do stuff with vendor
       }
   }
}
于 2013-10-08T14:55:07.177 回答