简体   繁体   English

c#Linq查询按组然后按子组对项目进行分组

[英]c# Linq query to group items by group and then sub-group

Question : I have a list of items which I need to first group and then sub group by the number of items in their product group. 问题:我有一个物品清单,我需要先按其产品组中的物品数量分组,然后再分组。 Code below. 下面的代码。 The objective is to create matching groups where the zones and the products available in each match. 目的是创建匹配组,其中每个区域中可用的区域和产品都匹配。 The zone and product are subject to change, but the number products available should always be grouped. 区域和产品可能会更改,但是可用产品的数量应始终分组。

for example. 例如。 given the below... 给定以下...

Result should be... 结果应该是...

Group 1
Zone = "EAST", Product = "Bananas", ShippingTime = "3_Days" };
Zone = "EAST", Product = "Oranges", ShippingTime = "5_Days" };
Zone = "SOUTH", Product = "Bananas", ShippingTime = "3_Days" };
Zone = "SOUTH", Product = "Oranges", ShippingTime = "10_Days" };

Group 2 2组

Zone = "WEST", Product = "Oranges", ShippingTime = "3_Days" };
Zone = "WEST", Product = "Oranges", ShippingTime = "5_Days" };
Zone = "WEST", Product = "Oranges", ShippingTime = "10_Days" };
Zone = "WEST", Product = "Apples", ShippingTime = "3_Days" };
Zone = "WEST", Product = "Apples", ShippingTime = "5_Days" };
Zone = "WEST", Product = "Apples", ShippingTime = "5_Days" };
Zone = "WEST", Product = "Bananas", ShippingTime = "3_Days" };
Zone = "WEST", Product = "Bananas", ShippingTime = "5_Days" };
Zone = "WEST", Product = "Bananas", ShippingTime = "5_Days" };

The ideal grouping would be to break out the below from Group 2 (caus' they match what's in group 1) and add to group 1, thus leaving the residual in group 2. 理想的分组方式是从组2中分解出以下内容(因为它们与组1中的内容匹配)并添加到组1中,因此将残差留在组2中。

Zone = "WEST", Product = "Bananas", ShippingTime = "3_Days" };
Zone = "WEST", Product = "Oranges", ShippingTime = "10_Days" };

Here's the test I've been working with. 这是我一直在使用的测试。 Nothing I've been able to do with linq seems to get the job done. 我对linq所做的一切似乎都无法完成。 Thanks in Advance for the ideas. 在此先感谢您的想法。

public void should_group_products_and_shippingtimes()
        {
            {
                Bananas = "Limited DR";
                var a = new MyClass { Id = 1, Zone = "EAST", Product = "Bananas", ShippingTime = "3_Days" };
                var b = new MyClass { Id = 2, Zone = "EAST", Product = "Oranges", ShippingTime = "5_Days" };
                var c = new MyClass { Id = 3, Zone = "SOUTH", Product = "Bananas", ShippingTime = "3_Days" };
                var d = new MyClass { Id = 4, Zone = "SOUTH", Product = "Oranges", ShippingTime = "10_Days" };
                var e = new MyClass { Id = 5, Zone = "WEST", Product = "Oranges", ShippingTime = "3_Days" };
                var f = new MyClass { Id = 6, Zone = "WEST", Product = "Oranges", ShippingTime = "5_Days" };
                var g = new MyClass { Id = 7, Zone = "WEST", Product = "Oranges", ShippingTime = "10_Days" };
                var h = new MyClass { Id = 8, Zone = "WEST", Product = "Apples", ShippingTime = "3_Days" };
                var i = new MyClass { Id = 9, Zone = "WEST", Product = "Apples", ShippingTime = "5_Days" };
                var j = new MyClass { Id = 10, Zone = "WEST", Product = "Apples", ShippingTime = "5_Days" };
                var k = new MyClass { Id = 11, Zone = "WEST", Product = "Bananas", ShippingTime = "3_Days" };
                var l = new MyClass { Id = 12, Zone = "WEST", Product = "Bananas", ShippingTime = "5_Days" };
                var m = new MyClass { Id = 13, Zone = "WEST", Product = "Bananas", ShippingTime = "5_Days" };
                var myList = new List<MyClass>();
                myList.AddRange(new[] {a,b,c,d,e,f,g,h,i,j,k,l,m});

                var sublist = (from ee in myList
                                                from ff in myList
                                                where ee.Product == ff.Product
                                                      && ee.Id != ff.Id
                                                select ee).Distinct();

                var match1 =
                       myList.AsEnumerable().GroupBy(
                           record =>
                           new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 1);

                var match2 =
                       myList.AsEnumerable().GroupBy(
                           record =>
                           new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 2);

                var match3 =
                       myList.AsEnumerable().GroupBy(
                           record =>
                           new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 3);

                var match4 =
                       myList.AsEnumerable().GroupBy(
                           record =>
                           new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 4);

                var match5 =
                       myList.AsEnumerable().GroupBy(
                           record =>
                           new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 5);

                // Get the total from each of these group where they match, throw the rest out.


                foreach (var entry in sublist)
                {
                    Console.WriteLine(entry);

                }

                Assert.That(sublist, Is.Not.Null);
            }
        } 

// Supporting Class //支持类

public class MyClass
    {
        public int Id { get; set; }
        public string Zone { get; set; }
        public string Product { get; set; }
        public string ShippingTime { get; set; }
    }

I'm not sure I understood you right, but I think you want to do something like that: 我不确定我是否理解正确,但我认为您想做这样的事情:

myList.Select(record => record.Product)
      .Distinct()
      .Select(p => new 
              { 
                  Product = p, 
                  Zones = myList.Where(r => r.Product == p)
                                .Select(r => r.Zone)
                                .Distinct() 
              })
      .GroupBy(an => an.Zones.Count())

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM