c# 数字拆分,求个思路
有这么两组数据,length不相等,我需要将pushOutRow 中下标的ExchangeQty 给到pullInRow 的New Qty中,如果大于,需要进行拆分到新行,fltDate同时也赋值过去
var pullInRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-01",ExchangeQty = 39000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A2", FltDate = "2023-12-22",ExchangeQty = 6000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A3", FltDate = "",ExchangeQty = 15000}
};
var pushOutRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 35000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ASD", FltDate = "2023-12-07",ExchangeQty = 25000}
};
//正确结果
var endRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-01", ExchangeQty = 39000,NewQty=35000,NewFltDate="2023-12-06"},//只有35000,全部分过去
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-01", ExchangeQty = 39000,NewQty=4000,NewFltDate="2023-12-07"},//39000-35000=4000(新增行)使用25000其中的4000
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A2", FltDate = "2023-12-22", ExchangeQty = 6000,NewQty=6000,NewFltDate="2023-12-07"},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A3", FltDate = "", ExchangeQty = 15000,NewQty=15000,NewFltDate="2023-12-07"}
};
public class POExchangeList
{
public Guid ID { get; set; }
public string CustomerName { get; set; }
public string FltDate { get; set; }
public int ExchangeQty { get; set; }
public string NewFltDate { get; set; }
public int NewQty { get; set; }
public string bindID { get; set; }
public int isExchange { get; set; }
}
在这段代码中,我只考虑了pushOutRow 第一行的ExchangeQty 是小于的,如果第二行小于就会出现问题
int deadQty = 0;
List<POExchangeList> pullTemp = new List<POExchangeList>();
foreach (var pushOut in pushOutRow)
{
foreach (var pullIn in pullInRow)
{
int pushOutQty = pushOut.ExchangeQty;
int pullInQty = pullIn.ExchangeQty;
if (pullIn.isExchange != 1)
{
if (pushOutQty < pullInQty)
{
pullIn.NewQty = pushOutQty;
pullIn.NewFltDate = pushOut.FltDate;
pullIn.isExchange = 1;
deadQty = pullInQty - pushOutQty;//4000
break;
}
else
{
pullIn.NewQty = pullIn.ExchangeQty;
pullIn.NewFltDate = pushOut.FltDate;
}
}
else
{
//是否还有剩余数
if (deadQty > 0)
{
pullTemp.Add(new POExchangeList
{
ID = new Guid(),
CustomerName = pullIn.CustomerName,
FltDate = pullIn.FltDate,
ExchangeQty = pullIn.ExchangeQty,
NewQty = deadQty,
NewFltDate = pushOut.FltDate,
isExchange = 1
});
deadQty -= pullIn.ExchangeQty;
}
}
}
}
pullInRow.AddRange(pullTemp);
本帖最后由 go4399 于 2023-11-24 15:50 编辑
```csharp
public static void POExchange(List<POExchangeList> pullInRow, List<POExchangeList> pushOutRow)
{
if (pullInRow.Count < 1 || pushOutRow.Count < 1)
{
return;
}
var i = 0;
var o = 0;
var inQty = pullInRow.ExchangeQty;
var outQty = pushOutRow.ExchangeQty;
while (inQty != 0 && outQty != 0)
{
if (outQty <= inQty)
{
pullInRow.NewQty = outQty;
pullInRow.NewFltDate = pushOutRow.FltDate;
pullInRow.isExchange = 1;
inQty -= outQty;
if (inQty > 0)
{
var newExchange = new POExchangeList
{
ID = Guid.NewGuid(),
CustomerName = pullInRow.CustomerName,
FltDate = pullInRow.FltDate,
ExchangeQty = pullInRow.ExchangeQty
};
pullInRow.Insert(++i, newExchange);
}
else
{
++i;
inQty = i < pullInRow.Count ? pullInRow.ExchangeQty : 0;
}
++o;
outQty = o < pushOutRow.Count ? pushOutRow.ExchangeQty : 0;
}
else // if (outQty > inQty)
{
pullInRow.NewQty = inQty;
pullInRow.NewFltDate = pushOutRow.FltDate;
pullInRow.isExchange = 1;
outQty -= inQty;
++i;
inQty = i < pullInRow.Count ? pullInRow.ExchangeQty : 0;
}
}
}
``` var pullInRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-01",ExchangeQty = 39000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A2", FltDate = "2023-12-22",ExchangeQty = 6000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A3", FltDate = "",ExchangeQty = 15000}
};
var pushOutRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 35000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ASD", FltDate = "2023-12-07",ExchangeQty = 25000}
};
List<POExchangeList> pushResult=new List<POExchangeList>();
for (int i = 0; i < pullInRow.Count;i++ )
{
if (i < pushOutRow.Count)
{
if (pullInRow.ExchangeQty > pushOutRow.ExchangeQty)
{
pushResult.Add(new POExchangeList
{
ID = new Guid(),
CustomerName = pullInRow.CustomerName,
FltDate = pullInRow.FltDate,
ExchangeQty = pullInRow.ExchangeQty,
NewQty = pushOutRow.ExchangeQty,
NewFltDate = pushOutRow.FltDate
});
pushResult.Add(new POExchangeList
{
ID = new Guid(),
CustomerName = pullInRow.CustomerName,
FltDate = pullInRow.FltDate,
ExchangeQty = pullInRow.ExchangeQty,
NewQty = pullInRow .ExchangeQty- pushOutRow.ExchangeQty,
NewFltDate = pushOutRow.FltDate
});
}
else
{
pushResult.Add(new POExchangeList
{
ID = new Guid(),
CustomerName = pullInRow.CustomerName,
FltDate = pullInRow.FltDate,
ExchangeQty = pullInRow.ExchangeQty,
NewQty = pullInRow.ExchangeQty,
NewFltDate = pullInRow.FltDate
});
}
}
else
{
pushResult.Add(new POExchangeList
{
ID = new Guid(),
CustomerName = pullInRow.CustomerName,
FltDate = pullInRow.FltDate,
ExchangeQty = pullInRow.ExchangeQty,
NewQty = pullInRow.ExchangeQty,
NewFltDate = pullInRow.FltDate
});
}
} 以后这种问题直接问GPT{:1_918:} simleXL 发表于 2023-11-24 16:12
var pullInRow = new List() {
new POExchangeList {ID=Guid ...
20-37行没有考虑pullInRow中1条记录会拆分成3条及以上的情况
36、48、61行NewFltDate赋值错误 go4399 发表于 2023-11-24 15:47
```csharp
public static void POExchange(List pullInRow, List pushOutRow)
{
大部分都是正常的,但有个疑问,当我的数据是以下内容的时候,endRow 有点异常
var pullInRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-01",ExchangeQty = 50000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-02",ExchangeQty = 4000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-03",ExchangeQty = 6000},
};
var pushOutRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 55000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-08",ExchangeQty = 4000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-07",ExchangeQty = 1000}
};
var endRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-01",ExchangeQty = 50000,NewQty=50000, NewFltDate="2024-12-06"},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-02",ExchangeQty = 4000,NewQty=4000, NewFltDate="2024-12-06"},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-03",ExchangeQty = 6000,NewQty = 1000, NewFltDate="2024-12-06"},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-03",ExchangeQty = 6000,NewQty=4000,NewFltDate="2023-12-08"},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-03",ExchangeQty = 6000,NewQty=1000,NewFltDate="2023-12-07"}
}; cn005897 发表于 2023-11-26 10:55
大部分都是正常的,但有个疑问,当我的数据是以下内容的时候,endRow 有点异常
这个endRow有什么异常 本帖最后由 cn005897 于 2023-11-26 14:52 编辑
go4399 发表于 2023-11-26 14:01
这个endRow有什么异常
应该是这样有问题,当pullInRow小于PushOutRow的长度,endRow(pullInRow )应该是有三条数据
var pullInRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-03",ExchangeQty = 6000},
};
var pushOutRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 1000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 3000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-07",ExchangeQty = 2000}
};
var endRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-03",ExchangeQty = 50000,NewQty=0, NewFltDate=""},
}; cn005897 发表于 2023-11-26 14:46
应该是这样有问题,当pullInRow小于PushOutRow的长度,endRow(pullInRow )应该是有三条数据
我测试的结果是
var endRow = new List<POExchangeList>() {
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-03",ExchangeQty = 6000, NewFltDate = "2023-12-06", NewQty = 1000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-03",ExchangeQty = 6000, NewFltDate = "2023-12-06", NewQty = 3000},
new POExchangeList {ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-03",ExchangeQty = 6000, NewFltDate = "2023-12-07", NewQty = 2000}
};
刚好拆成3行,你要求的正确结果是什么 cn005897 发表于 2023-11-26 14:46
应该是这样有问题,当pullInRow小于PushOutRow的长度,endRow(pullInRow )应该是有三条数据
以下是完整测试代码,可以运行看结果
```csharp
using System;
using System.Collections.Generic;
namespace Test
{
internal static class Program
{
private static void Main(string[] args)
{
var pullInRow = new List<POExchangeList>()
{
new POExchangeList { ID = Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-03", ExchangeQty = 6000 }
};
var pushOutRow = new List<POExchangeList>()
{
new POExchangeList { ID = Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06", ExchangeQty = 1000 },
new POExchangeList { ID = Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06", ExchangeQty = 3000 },
new POExchangeList { ID = Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-07", ExchangeQty = 2000 }
};
POExchange(pullInRow, pushOutRow);
Print(pullInRow);
Console.ReadKey();
}
public class POExchangeList
{
public Guid ID { get; set; }
public string CustomerName { get; set; }
public string FltDate { get; set; }
public int ExchangeQty { get; set; }
public string NewFltDate { get; set; }
public int NewQty { get; set; }
public string bindID { get; set; }
public int isExchange { get; set; }
}
public static void Print(List<POExchangeList> list)
{
Console.WriteLine("var endRow = new List<POExchangeList>() {");
foreach (var exchangeList in list)
{
Print(exchangeList);
}
Console.WriteLine("};");
}
public static void Print(POExchangeList exchangeList)
{
Console.WriteLine($" new POExchangeList {{ ID = Guid.NewGuid(), CustomerName = \"{exchangeList.CustomerName}\", FltDate = \"{exchangeList.FltDate}\", ExchangeQty = {exchangeList.ExchangeQty}, NewFltDate = \"{exchangeList.NewFltDate}\", NewQty = {exchangeList.NewQty} }};");
}
public static void POExchange(List<POExchangeList> pullInRow, List<POExchangeList> pushOutRow)
{
if (pullInRow.Count < 1 || pushOutRow.Count < 1)
{
return;
}
var i = 0;
var o = 0;
var inQty = pullInRow.ExchangeQty;
var outQty = pushOutRow.ExchangeQty;
while (inQty != 0 && outQty != 0)
{
if (outQty <= inQty)
{
pullInRow.NewQty = outQty;
pullInRow.NewFltDate = pushOutRow.FltDate;
pullInRow.isExchange = 1;
inQty -= outQty;
if (inQty > 0)
{
var newExchange = new POExchangeList
{
ID = Guid.NewGuid(),
CustomerName = pullInRow.CustomerName,
FltDate = pullInRow.FltDate,
ExchangeQty = pullInRow.ExchangeQty
};
pullInRow.Insert(++i, newExchange);
}
else
{
++i;
inQty = i < pullInRow.Count ? pullInRow.ExchangeQty : 0;
}
++o;
outQty = o < pushOutRow.Count ? pushOutRow.ExchangeQty : 0;
}
else // if (outQty > inQty)
{
pullInRow.NewQty = inQty;
pullInRow.NewFltDate = pushOutRow.FltDate;
pullInRow.isExchange = 1;
outQty -= inQty;
++i;
inQty = i < pullInRow.Count ? pullInRow.ExchangeQty : 0;
}
}
}
}
}
```
页:
[1]