如果值相同,则将值重新编号为

时间:2016-10-21 14:57:22

标签: c#

我正在尝试重新编号列表中的值,该列表将动态地传递给我。我已经进入了第三类叫做#34;混合"而且我不确定如何做到这一点。

"混合" category将有一个可能相同的palletno。如果该值是相同的,我需要它做的是增加1,我正在做但我需要该类别的起始值为tempPN值(在此列表中将为7)。我尝试了不同的方法,但这是我能得到的最接近的方法。任何的意见都将会有帮助。

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

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

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

        }

答案 1 :(得分:0)

如果旧号码不重要......(这将完全重新排序)

var pallets = GetPalletNumbers()
    .GroupBy(p => p.dropno)
    .Select(g => new
    {
        DropGroup = g.Key,
        Pallets = g.OrderBy(p => p.palletstatus == "Full" ? 1 
                               : p.palletstatus == "Partial" ? 2 
                               : 3) //weights
                   .Select((p, i) => { p.palletno = i + 1; return p; })
                   // would be better to create a new object versus mutate but this works
                   .ToList()
    })
    .ToList();