what I'm trying to do is grouping by product and ensure same product is schedule to be counted on same days always, the daysScheduled can not be more than product types MILK,CHAIR,TABLE, TV = 4 product types and max scheduledDays would be 4
I have a list of products and I want to count same products on same days
int scheduledDays = 2;
IList<Product> products = new List<Product>();
products.Add(new Product("MILK", "123"));
products.Add(new Product("MILK", "123"));
products.Add(new Product("CHAIR", "456"));
products.Add(new Product("CHAIR", "456"));
products.Add(new Product("CHAIR", "456"));
products.Add(new Product("TABLE", "234"));
products.Add(new Product("TABLE", "234"));
products.Add(new Product("TV", "567"));
EXPECTED if scheduledDays is 2, the products to be counted the first day will be, notice that scheduledDays values could be 1,2,3, etc and the list of product could have another items as well
PRODUCTS TO BE COUNTED DAY 1 PRODUCTS TO BE COUNTED DAY 2
products.Add(new Product("MILK", "123")); | products.Add(new Product("TABLE", "234"));
products.Add(new Product("MILK", "123")); | products.Add(new Product("TABLE", "234"));
products.Add(new Product("CHAIR", "456")); | products.Add(new Product("TV", "567"));
products.Add(new Product("CHAIR", "456")); |
products.Add(new Product("CHAIR", "456")); |
EXPECTED if scheduledDays is 3, the products to be counted the first day will be, notice that scheduledDays values could be 1,2,3, etc and the list of product could have another items as well
PRODUCTS TO BE COUNTED DAY 1 PRODUCTS TO BE COUNTED DAY 3
products.Add(new Product("MILK", "123")); | products.Add(new Product("TABLE", "234"));
products.Add(new Product("MILK", "123")); | products.Add(new Product("TABLE", "234"));
| products.Add(new Product("TV", "567"));
PRODUCTS TO BE COUNTED DAY 2
products.Add(new Product("CHAIR", "456")); |
products.Add(new Product("CHAIR", "456")); |
products.Add(new Product("CHAIR", "456")); |
Ensure same product is scheduled to be counted on same day always, I was trying to use groupby something like this, but I do not know how to split with scheduledDays, I'm completely blocked, please help
int scheduledDays = 2;
var lists = products.GroupBy(x => x.Product);
foreach (var group in lists)
{}
CodePudding user response:
you are in the right direction, you just need to run a loop.
The trick is to skip the last previous groups items and then take the scheduledDays
items.
foreach (var group in lists)
{
for (int i = 0; i < scheduledDays; i )
{
days.Add(group.Skip(i * scheduledDays).Take(scheduledDays).ToList());
}
}