简体   繁体   中英

how can I Optimize this foreach

        public int SetNumber(FinancialEntities db, bool Redirected = false)
        {
            db = new FinancialEntities();
            List<Document> ds = db.Documents.Where(d => DbFunctions.TruncateTime(d.Date) > DbFunctions.TruncateTime(Date) && d.UnitID == UnitID).ToList();
            ds.Remove(this);
            ds = ds.OrderBy(d => d.Date).ToList();
            if (ds.Count > 0)
            {
                Number = ds.FirstOrDefault().Number;
                int tn = (int)Number;
                foreach (var item in ds)
                {
                    tn++;
                    if (item.Number != tn)
                    {
                        item.Number = tn;
                        db.Entry(item).State = System.Data.Entity.EntityState.Modified;
                    }
                }
                db.SaveChanges();
            }
            else
            {
                Number = db.Documents.Where(d => d.UnitID == UnitID).Count() > 0 ? db.Documents.Where(d => d.UnitID == UnitID).Max(d => d.Number) + 1 : 1;
            }
            List<int?> nums = db.Documents.Where(d => d.UnitID == UnitID).Select(d => d.Number).ToList();
            int nc = nums.Count();
            int ndc = nums.Distinct().Count();
            if (nums.Distinct().Count() != nums.Count() && Redirected == false)
            {
                if (ds.Count() == 0)
                    ds = db.Documents.Where(d => d.UnitID == UnitID).ToList();
                ds.First().SetNumber(db, true);
            }
            return (int)Number;
        }

this is a method that puts a number at the beginning of a date and fixes the rest of the entries after that date. it takes a long time to eterate through the entries and is causing a lot of problems. how can I optimize this?

You can try to remove som much reads from DB and perform all operations in memory.

Something like this:

 public int SetNumber(FinancialEntities db, bool Redirected = false)
    {
        db = new FinancialEntities();
        var docs = db.Documents.Where(d => d.UnitID == UnitID).ToList();
        var ds = docs.Where(d => d.Date > Date).ToList();
        ds.Remove(this);
        ds = ds.OrderBy(d => d.Date).ToList();
        if (ds.Count > 0)
        {
            Number = ds.FirstOrDefault().Number;
            int tn = (int)Number;
            foreach (var item in ds)
            {
                tn++;
                if (item.Number != tn)
                {
                    item.Number = tn;
                    db.Entry(item).State = System.Data.Entity.EntityState.Modified;
                }
            }
            db.SaveChanges();
        }
        else
        {
            Number = docs.Count > 0 ? docs.Max(d => d.Number) + 1 : 1;
        }

        List<int?> nums = docs.Select(d => d.Number).ToList();
        int nc = nums.Count;
        int ndc = nums.Distinct().Count();
        if (ndc != nc && Redirected == false)
        {
            if (ds.Count == 0)
                ds = docs.ToList();
            ds.First().SetNumber(db, true);
        }
        return (int)Number;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM