Linq总计基于位置

时间:2015-10-06 12:04:32

标签: c# .net linq

我有以下代码,它可以返回每个位置共有库存的记录列表。在我进一步研究之前,我正在尝试清理程序。有没有办法用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;
}

1 个答案:

答案 0 :(得分:0)

这将非常接近,但是,如果您提供的代码没有记录,则不会返回所有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();
}