public BindableCollection <SACH> GetNewBookList()
        {
            BindableCollection <SACH>   BookList;
            BindableCollection <CTSACH> ctsList;

            using (QLTVEntities db = new QLTVEntities())
            {
                BookList = new BindableCollection <SACH>(db.SACH.Include(x => x.CTSACH).Where(y => true));
                ctsList  = new BindableCollection <CTSACH>(db.CTSACH.Include(x => x.SACH.TACGIA).Include(x => x.SACH.THELOAI).Include(x => x.SACH.NHAXUATBAN).Include(x => x.PHIEUNHAPSACH).OrderByDescending(x => x.MACTS));
                BookList = new BindableCollection <SACH>(ctsList.GroupBy(x => x.SACH).Select(p => p.Key));
                if (BookList.Any())
                {
                    foreach (SACH s in BookList)
                    {
                        int c = 0;
                        if (s.CTSACH is null)
                        {
                            continue;
                        }
                        foreach (CTSACH cts in s.CTSACH)
                        {
                            CTSACH t = db.CTSACH.Include(x => x.PHIEUMUONSACH).FirstOrDefault(p => p.MACTS == cts.MACTS);
                            if (t is null)
                            {
                                continue;
                            }
                            c += t.PHIEUMUONSACH.Count();
                        }
                        s.LUOTMUON = c;
                    }
                }
            }
            return(BookList);
        }
        public BindableCollection <SACH> GetBookList(Expression <Func <SACH, bool> > currentEntityFilter)
        {
            BindableCollection <SACH> BookList = new BindableCollection <SACH>();

            using (QLTVEntities db = new QLTVEntities())
            {
                BookList = new BindableCollection <SACH>(db.SACH.Include(x => x.TACGIA).Include(x => x.THELOAI).Include(x => x.NHAXUATBAN).Include(x => x.CTSACH).Where(currentEntityFilter));
                if (BookList.Any())
                {
                    foreach (SACH s in BookList)
                    {
                        int c = 0;
                        if (s.CTSACH is null)
                        {
                            continue;
                        }
                        foreach (CTSACH cts in s.CTSACH)
                        {
                            CTSACH t = db.CTSACH.Include(x => x.PHIEUMUONSACH).FirstOrDefault(p => p.MACTS == cts.MACTS);
                            if (t is null)
                            {
                                continue;
                            }
                            c += t.PHIEUMUONSACH.Count();
                        }
                        s.LUOTMUON = c;
                    }
                }
            }
            return(BookList);
        }
 public void UpdateCTS(CTSACH cs, PHIEUMUONSACH pms)
 {
     using (QLTVEntities db = new QLTVEntities())
     {
         CTSACH        c = db.CTSACH.FirstOrDefault(x => x.MACTS == cs.MACTS);
         PHIEUMUONSACH d = db.PHIEUMUONSACH.FirstOrDefault(x => x.MAPMS == pms.MAPMS);
         c.MATT = cs.MATT;
         c.PHIEUMUONSACH.Add(d);
         d.CTSACH.Add(c);
         db.SaveChanges();
     }
 }