Sean Schroder Sean Schroder - 1 month ago 10
C# Question

re-number a value by 1 if same value

I am trying to re-number a value in a list that will come dynamically to me. I have gotten to the third category called "Mixed" and I not sure how to quite do this.

The "Mixed" category will have a palletno that could be the same. If that value is the same all I need it to do is to increase by 1 which I am doing but I need the starting value for that category to be the tempPN value(which in this list would be 7). I have tried different ways but this is the closest I can get it. Any suggestions would be helpful.

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public class PalletNumberRepository
{
public class PalletNumber
{
public int palletno { get; set; }
public decimal weight { get; set; }
public decimal totalwieght {get; set;}
public string palletstatus {get; set;}
public string dropno {get; set;}

}

public List<PalletNumber> GetPalletNumbers()
{
return new List<PalletNumber>
{
new PalletNumber(){ palletno = 3, weight = 2575m, totalwieght = 3667m, palletstatus = "Mixed", dropno = "1B"},
new PalletNumber(){ palletno = 3, weight = 2575m, totalwieght = 3667m, palletstatus = "Mixed", dropno = "1B"},
new PalletNumber(){ palletno = 3, weight = 2575m, totalwieght = 3667m, palletstatus = "Mixed", dropno = "1B"},
new PalletNumber(){ palletno = 4, weight = 92m, totalwieght = 2667m, palletstatus = "Mixed", dropno = "1B"},
new PalletNumber(){ palletno = 4, weight = 92m, totalwieght = 2667m, palletstatus = "Mixed", dropno = "1B"},
new PalletNumber(){ palletno = 6, weight = 1200m, totalwieght = 1840m, palletstatus = "Full", dropno = "1B"},
new PalletNumber(){ palletno = 8, weight = 640m, totalwieght = 2800m, palletstatus = "Partial", dropno = "1B"},
new PalletNumber(){ palletno = 8, weight = 640m, totalwieght = 2800m, palletstatus = "Partial", dropno = "1B"}
};
}
}
public static void Main()
{
var pnlist = new PalletNumberRepository().GetPalletNumbers();
var firstPalletNumber = Convert.ToInt32(pnlist.First().palletno);
var tempPN = 0;
var pn = pnlist
.OrderByDescending(i => i.palletstatus.StartsWith("Full"))
.ThenByDescending(i => i.palletstatus.StartsWith("Partial"))
.GroupBy(i => i.dropno)
.ToList();

foreach(var item in pn)
{
Console.WriteLine(item.Key);

foreach(var i in item)
{
if(i.palletstatus == "Full")
{
i.palletno = firstPalletNumber;
tempPN = i.palletno + 1;
firstPalletNumber++;
Console.WriteLine(i.palletno + " Pallet Status: " + i.palletstatus);
}

else if (i.palletstatus == "Partial")
{
i.palletno = firstPalletNumber;
tempPN = i.palletno + 1;
firstPalletNumber++;
Console.WriteLine(i.palletno + " Pallet Status: " + i.palletstatus);
}

else if (i.palletstatus == "Mixed")
{
// This is were I I need the tempPN
//(7 in this run) to replace all the 4's
//then step up to 8 for the 5's. So on and so on.

//the list is going to always be different

i.palletno += 1;
Console.WriteLine(i.palletno + " Pallet Status: " + i.palletstatus);
}
}
}
}
}

Answer

Is this what you want?

        static void Main(string[] args)
        {
            List<string> order = new List<string>() { "Full", "Partial", "Mixed" };
            var pnlist = new PalletNumberRepository().GetPalletNumbers();
            var groups = pnlist.GroupBy(x => new { x.palletstatus, x.palletno }).OrderBy(x => order.IndexOf(x.Key.palletstatus)).ThenBy(x => x.Key.palletno).ToList();
            int index = pnlist.Min(x => x.palletno);
            foreach (var group in groups)
            {
                foreach (PalletNumberRepository.PalletNumber pallet in group)
                {
                    pallet.palletno = index;
                }
                index++;
            }

        }
Comments