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

用户名

问题:我有一个项目清单,我需要先按其产品组中的项目数分组,然后再进行子分组。下面的代码。目的是创建匹配组,其中每个区域中可用的区域和产品都匹配。区域和产品可能会更改,但是可用产品的数量应始终分组。

例如。给定以下...

结果应该是...

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" };

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" };

理想的分组方式是从组2中分解出以下内容(因为它们与组1中的内容匹配)并添加到组1中,因此将残差留在组2中。

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

这是我一直在使用的测试。我对linq所做的一切似乎都无法完成。在此先感谢您的想法。

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);
            }
        } 

//支持类

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

我不确定我是否理解正确,但我认为您想做这样的事情:

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())

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

MySQL查询计数结果大于10,然后按组

计算2个子查询然后按日期分组-MySQL

AngularJS-ngOptions:如何按组名然后按标签排序

按对象值分组,计数然后按最大对象属性设置组密钥

如何按linq查询对组中的元素进行排序,然后选择第一个?

LINQ:分组子组

非股权加入,然后按组汇总

按组中的位置对数据框进行排序,然后按该组

组将javascript数组中的值重复在一起,然后按其值名称升序对这些组进行排序

C#按列表分组,然后按SUM

在每个组中按子组汇总(和分组依据)

LINQ按日期分组,然后按属性排序,以获取最高金额?

C#LINQ按如下所示的格式对对象列表进行分组

C#Linq按对象分组

将一组数字列求和,然后按组折叠字符串

查询子文档上的汇总,然后按父文档中的字段分组

如何进行汇总,然后按组乘以结果?

LINQ查询以按顺序在非组对象之间按属性组对对象进行计数

使用Linq分组如何按特定组排序,然后其余组降序

按正则表达式过滤powershell中的字符串列表,然后按捕获组之一进行分组和排序

C#LINQ按小时从数据表组获取记录

首先按C列分组,然后按A列分组

Excel-按列对行进行分组,然后按另一列进行排序

首先按固定顺序的子组对data.frame列进行重新排序,然后按字母顺序在每个子组内重新排序

使用自定义列的SQL SELECT查询,然后按列进行汇总和分组

查询以循环遍历一个表,然后按组递增地插入另一个表

复杂组按linq查询

子查询对项目进行计数,然后按主查询的一个字段对它们进行分组,没有重复

C# LINQ 按属性组集合,然后按列表定义的显式顺序对每个组进行排序