public TuVungCollection SelectAllTuVung_CongDong()
        {
            //Lấy từ vựng trong từ điển cộng đồng
            hoctuvungLINQDataContext db = new hoctuvungLINQDataContext();
            TuVungCollection tuvungColl = new TuVungCollection();
            IEnumerable<TuVung> TuVungList;
            TuVungList = from p in db.TuVungs
                             where p.TuDien.CongDong == true
                             && p.kiemduyet == true                           
                             select p;
           
            //Addrange
            foreach (TuVung tv in TuVungList)
            {
                TuVungBO tvBO = new TuVungBO();
                tvBO.TuVungID = tv.TuVungID.ToString();
                tvBO.TuVung = tv.TuVung1;
                tvBO.LoaiTuID = (int)tv.LoaiTuID;
                tvBO.NghiaTu = tv.NghiaTu;
                tvBO.ViDu = tv.Vidu;
                tvBO.TaiKhoan = tv.taikhoan;
                tvBO.HinhAnh = tv.Hinhanh;
                tvBO.SoLanSai = (int)tv.SoLanSai;
                tvBO.SoLanSuDung = (int)tv.SoLanSuDung;
                tuvungColl.Add(tvBO);
            }
            return tuvungColl;

        }
 public void LoadchitietTuVung(string tuvungID)
 {
     if (tuvungID == "")
     {
         TuTextBox.Text = "";
         TuDienDropDown1.SelectedIndex = 0;
         LoaiTuDropDown.SelectedIndex = 0;
         NghiaTuTextBox.Text = "";
         HinhAnhImage.ImageUrl = "~/images/no_image.jpg";
         ViDuTextBox.Text = "";
         NguoiDangLabel.Text = "";
     }
     else
     {
         //   ViewState["tuvungID"] = tuvungID;
         TuVungBO tvBO = new TuVungBO();
         tvBO = tuvungBUS.Lay_ChiTiet_TuVung(tuvungID);
         TuTextBox.Text = tvBO.TuVung;
         TuDienDropDown1.SelectedValue = tvBO.TuDienID.ToString();
         LoaiTuDropDown.SelectedValue = tvBO.LoaiTuID.ToString();
         NghiaTuTextBox.Text = tvBO.NghiaTu;
         //Load hình ảnh
         if (tvBO.HinhAnh != "")
             HinhAnhImage.ImageUrl = tvBO.HinhAnh;
         else
             HinhAnhImage.ImageUrl = "~/images/no_image.jpg";
         ViDuTextBox.Text = tvBO.ViDu;
         NguoiDangLabel.Text = tvBO.TaiKhoan;
     }
 }
 private bool KiemTraTrungCauTraLoi(TuVungBO[] tuvung, TuVungBO temp, int n)
 {
     for (int i = 0; i < n; i++)
     {
         if (temp == tuvung[i])
             return true;
     }
     return false;
 }
 private bool KiemTraTrungCauHoi(TracNghiemCollection cauhoiColl, TuVungBO tv)
 {
     foreach (TracNghiemBO tn in cauhoiColl)
     {
         if (tv.TuVungID == tn.Choices[0].TuVungID)
             return true;
     }
     return false;
 }
 public bool Compare(TuVungBO tuvungBO)
 {
     if (this.LoaiTuID == tuvungBO.LoaiTuID
         && this.TuVung == tuvungBO.TuVung
         && this.NghiaTu == tuvungBO.NghiaTu
         && this.ViDu == tuvungBO.ViDu
         && this.TaiKhoan == tuvungBO.TaiKhoan)
         return true;
     return false;
 }
        public string [,]TaoOChu(TuVungCollection tuvunglambaitest,ref int len,ref TuVungCollection GoiYOChu)
        {
            try
            {
                //B1:Lấy ra một từ ngẫunhiên làm từ hàng dọc
                string tuhangdoc = LayChuHangDoc(tuvunglambaitest);
                List<char> TuHangDoc = new List<char>();//chuyển string thành List<string> để xử lý           
                TuHangDoc = tuhangdoc.ToList();

                //B2: Lấy ra các từ hàng ngang
                TuVungCollection HangNgangList = new TuVungCollection();//Lưu các đối tượng tuvungBO hàng ngang
                List<string> HangNgang = new List<string>(); //Lưu các từ hàng ngang
                int i = 0;
                int solanloop = 0;
                while (i < TuHangDoc.Count)
                {
                    TuVungBO tuhangnganBO = new TuVungBO();
                    tuhangnganBO = LayChuHangNgang(tuvunglambaitest, TuHangDoc[i]);
                    if (tuhangnganBO == null) return null;
                    string tuhangngang = tuhangnganBO.TuVung;
                    if (KiemTraHopLe(tuhangngang, TuHangDoc[i]) == true 
                        && KiemTraTrungTuDoc(tuhangdoc, tuhangngang) == false 
                        && KiemTraTrungTuNgang(HangNgang, tuhangngang) == false)
                    {
                        HangNgang.Add(tuhangnganBO.TuVung);
                        HangNgangList.Add(tuhangnganBO);
                        i++;
                        solanloop = 0;
                    }
                    solanloop++;
                    if (solanloop > 30)
                        return null;
                }
                //B3: Bố trí ô chữ lên mảng 2 chiều [12,21]
                len = TuHangDoc.Count;
                GoiYOChu = HangNgangList;
                return SapxepOChu(TuHangDoc,HangNgangList) ;
            }
            catch
            {
                return null;
            }

        }
        public bool CapNhatTuVung(string tuvungID,int LoaiTuID, string TuVung, string NghiaTu, string Hinhanh, string ViDu, string TaiKhoan,
            int SoLanSuDung, int SoLanSai, string TuDienID,bool KiemDuyet, int to, bool hasimage)
        {
            try
            {

                TuVungBO tuvungBO = new TuVungBO();
                tuvungBO.TuVungID = tuvungID;
                tuvungBO.LoaiTuID = LoaiTuID;
                tuvungBO.TuVung = TuVung;
                tuvungBO.NghiaTu = NghiaTu;
                tuvungBO.HinhAnh = Hinhanh;
                tuvungBO.ViDu = ViDu;
                tuvungBO.TaiKhoan = TaiKhoan;
                tuvungBO.SoLanSai = SoLanSai;
                tuvungBO.SoLanSuDung = SoLanSuDung;
                tuvungBO.TuDienID = TuDienID;
                tuvungBO.KiemDuyet = KiemDuyet;
                tuvungDAO.UpDateTuVung(tuvungBO, to,hasimage);
                return true;
            }
            catch
            {
                return false;
            }
        }
 public bool ThemTuVung(int LoaiTuID, string TuVung, string NghiaTu, string Hinhanh, string ViDu, string TaiKhoan,
   int SoLanSuDung, int SoLanSai, string TuDienID, bool KiemDuyet)
 {
     try
     {
         TuVungBO tuvungBO = new TuVungBO();
         tuvungBO.LoaiTuID = LoaiTuID;
         tuvungBO.TuVung = TuVung;
         tuvungBO.NghiaTu = NghiaTu;
         tuvungBO.HinhAnh = Hinhanh;
         tuvungBO.ViDu = ViDu;
         tuvungBO.TaiKhoan = TaiKhoan;
         tuvungBO.SoLanSai = SoLanSai;
         tuvungBO.SoLanSuDung = SoLanSuDung;
         tuvungBO.TuDienID = TuDienID;
         tuvungBO.KiemDuyet = KiemDuyet;
         tuvungDAO.InsertTuVung(tuvungBO);
         return true;
     }
     catch
     {
         return false;
     }
 }
 public void Add(TuVungBO tuvungBO)
 {
     List.Add(tuvungBO);
 }
 public void UpDateTuVung(TuVungBO tuvungBO,int to,bool hasimage)
 {
     hoctuvungLINQDataContext db = new hoctuvungLINQDataContext();
     Guid myguid = new Guid(tuvungBO.TuVungID);
     TuVung tv = db.TuVungs.SingleOrDefault(p=>p.TuVungID==myguid);    
     tv.TuVung1 = tuvungBO.TuVung;
     if(hasimage==true)
         tv.Hinhanh = tuvungBO.HinhAnh;
     tv.LoaiTuID = tuvungBO.LoaiTuID;
     tv.NghiaTu = tuvungBO.NghiaTu;
     tv.SoLanSai = tuvungBO.SoLanSai;
     tv.SoLanSuDung = tuvungBO.SoLanSuDung;
     tv.Vidu = tuvungBO.ViDu;
     Guid myguid1 = new Guid(tuvungBO.TuDienID);
     tv.TuDienID = myguid1;
     tv.taikhoan = tuvungBO.TaiKhoan;    
     //Insert vào từ điển cộng đồng
     if (to == 3)
     {
         tv.kiemduyet = tuvungBO.KiemDuyet;
     }
     db.SubmitChanges(); 
 }
 public void InsertTuVung(TuVungBO tuvungBO)
 {
     hoctuvungLINQDataContext db = new hoctuvungLINQDataContext();
     TuVung tv = new TuVung();
     tv.TuVungID = Guid.NewGuid();
     tv.TuVung1 = tuvungBO.TuVung;
     tv.Hinhanh = tuvungBO.HinhAnh;
     tv.LoaiTuID = tuvungBO.LoaiTuID;
     tv.NghiaTu = tuvungBO.NghiaTu;
     tv.SoLanSai = tuvungBO.SoLanSai;
     tv.SoLanSuDung = tuvungBO.SoLanSuDung;
     tv.Vidu = tuvungBO.ViDu;
     Guid myguid = new Guid(tuvungBO.TuDienID);
     tv.TuDienID = myguid;
     tv.taikhoan = tuvungBO.TaiKhoan;
     tv.kiemduyet = true;
     db.TuVungs.InsertOnSubmit(tv);
     db.SubmitChanges();
 }
  public void InsertTuVung(TuVungBO tuvungBO, int to)
  {          
      hoctuvungLINQDataContext db= new hoctuvungLINQDataContext();
      TuVung tv = new TuVung();
      tv.TuVungID = Guid.NewGuid();
      tv.TuVung1 = tuvungBO.TuVung;
      tv.Hinhanh = tuvungBO.HinhAnh;
      tv.LoaiTuID = tuvungBO.LoaiTuID;
      tv.NghiaTu = tuvungBO.NghiaTu;
      tv.SoLanSai = tuvungBO.SoLanSai;
      tv.SoLanSuDung = tuvungBO.SoLanSuDung;            
      tv.Vidu = tuvungBO.ViDu;
      Guid myguid = new Guid(tuvungBO.TuDienID);
      tv.TuDienID = myguid;
      tv.taikhoan = tuvungBO.TaiKhoan;
      //Insert vào từ điển cá nhân   //Insert vào từ điển đựơc sử dụng chung
      if (to == 1 || to == 2)
      {
          db.TuVungs.InsertOnSubmit(tv);
      }         
 
      //Insert vào từ điển cộng đồng
      else if (to == 3)
      {
          tv.kiemduyet = false;
      }
      db.TuVungs.InsertOnSubmit(tv);
      db.SubmitChanges(); 
  }
 /// <summary>
 /// ////Các hàm trong quanlytuvung.aspx
 /// </summary>   
 public TuVungBO SelectOneTuVung(string tuvungID)
 {
     hoctuvungLINQDataContext db = new hoctuvungLINQDataContext();
     TuVungBO tvBO = new TuVungBO();
     Guid myguid = new Guid(tuvungID);
     TuVung tv = db.TuVungs.SingleOrDefault(p => p.TuVungID == myguid);
     tvBO.TuVungID = tv.TuVungID.ToString();
     tvBO.TuDienID = tv.TuDienID.ToString();
     tvBO.TuVung = tv.TuVung1;
     tvBO.LoaiTuID = (int)tv.LoaiTuID;
     tvBO.NghiaTu = tv.NghiaTu;
     tvBO.ViDu = tv.Vidu;
     tvBO.TaiKhoan = tv.taikhoan;
     tvBO.HinhAnh = tv.Hinhanh;
     tvBO.SoLanSai = (int)tv.SoLanSai;
     tvBO.SoLanSuDung = (int)tv.SoLanSuDung;
     return tvBO;
 }
        public TuVungCollection SelectTuVung_LienKet(string tuvung, string taikhoan, string tudienID)
        {
            //Lấy từ vựng trong từ điển liên kết

            hoctuvungLINQDataContext db = new hoctuvungLINQDataContext();
            TuVungCollection tuvungColl = new TuVungCollection();
            Guid myguid= new Guid(tudienID);
            IEnumerable<Nhom_TuDien> nhomtudienList=from p in db.Nhom_TuDiens
                                                    where p.taikhoan==taikhoan
                                                    select p;
            IEnumerable<TuVung> TuVungList;
            if (tuvung == "")
            {              
                TuVungList = from p in db.TuVungs
                             where p.TuDienID==myguid
                             select p;
            }
            else
            {
                TuVungList = from p in db.TuVungs
                             //  where p.TuDien.Nhom_TuDiens == nhomtudien && 
                             where p.TuVung1.Contains(tuvung)
                             && p.TuDienID == myguid
                             select p;
            }
            //Addrange
            foreach (TuVung tv in TuVungList)
            {
                foreach (Nhom_TuDien nhomtd in nhomtudienList)
                {
                    if(tv.TuDienID==nhomtd.TuDienID)
                    { 
                        TuVungBO tvBO = new TuVungBO();
                        tvBO.TuVungID = tv.TuVungID.ToString();
                        tvBO.TuVung = tv.TuVung1;
                        tvBO.LoaiTuID = (int)tv.LoaiTuID;
                        tvBO.NghiaTu = tv.NghiaTu;
                        tvBO.ViDu = tv.Vidu;
                        tvBO.TaiKhoan = tv.taikhoan;
                        tvBO.HinhAnh = tv.Hinhanh;
                        tvBO.SoLanSai = (int)tv.SoLanSai;
                        tvBO.SoLanSuDung = (int)tv.SoLanSuDung;
                        tuvungColl.Add(tvBO);
                    }
                }
            }
            return tuvungColl;
        }