基于位置的Linq总数
本文关键字:Linq 总数 位置 于位置 | 更新日期: 2023-09-27 18:16:18
我有下面的代码,它的工作是返回一个记录列表,总计在每个位置的库存。在我进一步发展之前,我想把这个项目清理干净。是否有一种方法可以用LINQ做同样的事情?我知道这很乱,我想把它改进一下。
感谢您的关注!
private static List < Stock_Paper_Record > Get_Final_List(List < Stock_Paper_Record > Finallist, List < Stock_Paper_Record > papr, List < string > Codes, List < string > locs) {
int ii = 0;
int totalg = 0;
int totall = 0;
int totalp = 0;
decimal gsm = 0;
decimal size1 = 0;
decimal size2 = 0;
int commited = 0;
int OnOrder = 0;
int THRESH = 0;
int iount = 0;
papr.Sort((x, y) => String.Compare(x.Code, y.Code));
Finallist.Sort((x, y) => String.Compare(x.Code, y.Code));
int ic = 0;
int it = 0;
List < string > ls = new List < string > ();
List < decimal > st = new List < decimal > ();
foreach(string s in Codes) {
for (int iii = 0; iii < papr.Count; iii++) {
if (s == papr[iii].Code && papr[iii].Location == "GLASGOW") {
totalg += Convert.ToInt32(papr[iii].Stock);
gsm = papr[iii].Grammage;
size1 = papr[iii].Size1;
size2 = papr[iii].Size2;
commited = papr[iii].Commited;
OnOrder = papr[iii].Onorderi;
THRESH = papr[iii].Threshholdi;
}
if (s == papr[iii].Code && papr[iii].Location == "LONDON") {
totall += Convert.ToInt32(papr[iii].Stock);
gsm = papr[iii].Grammage;
size1 = papr[iii].Size1;
size2 = papr[iii].Size2;
commited = papr[iii].Commited;
OnOrder = papr[iii].Onorderi;
THRESH = papr[iii].Threshholdi;
}
if (s == papr[iii].Code && papr[iii].Location == "PARIS") {
totalp += Convert.ToInt32(papr[iii].Stock);
gsm = papr[iii].Grammage;
size1 = papr[iii].Size1;
size2 = papr[iii].Size2;
commited = papr[iii].Commited;
OnOrder = papr[iii].Onorderi;
THRESH = papr[iii].Threshholdi;
} else {}
}
Finallist.Add(new Stock_Paper_Record() {
Location = "BOTH", Code = Codes[iount], Stocki = totalg + totall + totalp + OnOrder, BBstocki = totalg, Lmstocki = totall, Ingstocki = totalp, Commited = commited, Ballance = (totalg + totall + totalp + OnOrder) - commited, Size1 = size1, Size2 = size2, Grammage = gsm, Threshholdi = THRESH, Onorderi = OnOrder
});
totalg = 0;
totall = 0;
totalp = 0;
ii++;
iount++;
}
return Finallist;
}
这将非常接近,但是,如果您提供一个没有记录的代码,这将不会返回一个全0的记录,但您的原始代码会。不知道这对你是否重要
private static List<Stock_Paper_Record> Get_Final_List(List<Stock_Paper_Record> Finallist, List<Stock_Paper_Record> papr, List<string> Codes, List<string> locs) {
return papr
.Where(p=>Codes.Contains(p.Location))
.GroupBy(p=>p.Code)
.Select(g=>new {
Code=g.Key,
BBstocki=g.Where(p=>p.Location=="GLASGOW").Sum(p=>Convert.ToInt32(p.Stock)),
Lmstocki=g.Where(p=>p.Location=="LONDON").Sum(p=>Convert.ToInt32(p.Stock)),
Ingstocki=g.Where(p=>p.Location=="PARIS").Sum(p=>Convert.ToInt32(p.Stock)),
Commited=g.FirstOrDefault().Commited,
Size1=g.FirstOrDefault().Size1,
Size2=g.FirstOrDefault().Size2,
Grammage=g.FirstOrDefault().Grammage,
Threshholdi=g.FirstOrDefault().Threshholdi,
Onorderi=g.FirstOrDefault().Onorderi
}).Select(g=>new Stock_Paper_Record {
Location="BOTH",
Code=g.Code,
BBstocki=g.BBstocki,
Lmstocki=g.Lmstocki,
Ingstocki=g.Ingstocki,
Commited=g.Commited,
Size1=g.Size1,
Size2=g.Size2,
Grammage=g.Grammage,
Threshholdi=g.Threshholdi,
Onorderi=g.Onorderi,
Stocki=g.BBstocki+g.Lmstocki+g.Ingstocki+g.Onorderi,
Ballance=g.BBstocki+g.Lmstocki+g.Ingstocki+g.Onorderi=g.Commited
})
.ToList();
}