首页 > 代码库 > Linq增删查改

Linq增删查改

//获取连接
        private OpalsoftUBDataDataContext db = new OpalsoftUBDataDataContext("Data Source=192.168.1.104;Initial Catalog=Opalsoft.UB;User ID=sa;Password=123");
        #region 添加产品的类型
        /// <summary>
        /// 添加产品的类型
        /// </summary>
        /// <param name="category">产品的类型对象</param>
        public void AddCategory(Category category)
        {
            db.Category.InsertOnSubmit(category);
            db.SubmitChanges();
        }
        #endregion

        #region 用户收藏,关注 产品类型
        /// <summary>
        /// 添加用户收藏,关注 产品类型
        /// </summary>
        /// <param name="bookmark">用户收藏产品类型对象</param>
        public void AddUserCategory(Bookmark bookmark)
        {
            db.Bookmark.InsertOnSubmit(bookmark);
            db.SubmitChanges();
        }
        #endregion

        #region 用户删除收藏产品类型
        /// <summary>
        /// 用户删除收藏产品类型
        /// </summary>
        /// <param name="bookmark">Bookmark 对象</param>
        public void DeleteUserCategory(Bookmark bookmark)
        {
            Bookmark newbookmark = db.Bookmark.Single(p => p.bm_id == bookmark.bm_id);
            newbookmark.status = "X";
            db.SubmitChanges();
        }
        #endregion

        #region 查询用户有没有添加过记录
        /// <summary>
        /// 查询用户有没有添加过记录
        /// </summary>
        /// <param name="UserID">用户id</param>
        /// <param name="bm_link_id">收藏关注id</param>
        /// <param name="type">类型</param>
        /// <returns>bool 是否有该记录</returns>
        public bool UserCategoryCount(int userID, int bm_link_id, string type)
        {
            bool temp = true;
            int annal = db.Bookmark.Count(p => p.user_id == userID && p.bm_link_id == bm_link_id && p.bm_link_type == type);
            if (annal != 0)
            {
                temp = false;
            }
            return temp;
        }
        #endregion

        #region 收藏产品改变状态为A
        /// <summary>
        /// 收藏产品改变状态为A
        /// </summary>
        /// <param name="bookmark">Bookmark 对象</param>
        public void UpdateUserCategory(Bookmark bookmark)
        {
            Bookmark newbookmark = db.Bookmark.Single(p => p.bm_id == bookmark.bm_id);
            newbookmark.status = "A";
            db.SubmitChanges();
        }
        #endregion

        #region 获取用户的关注的收藏
        /// <summary>
        /// 获取用户的关注的收藏
        /// </summary>
        /// <param name="userID">用户id</param>
        /// <param name="type">类型</param>
        /// <returns>list集合</returns>
        public List<Bookmark> GetUserUserCategory(int userID, string type)
        {
            List<Bookmark> list = db.Bookmark.Where(p => p.user_id == userID && p.bm_link_type == type && p.status == "A").ToList<Bookmark>();
            return list;
        }
        #endregion

        #region 获取上一级产品类型
        /// <summary>
        /// 获取上一级产品类型
        /// </summary>
        /// <param name="UpCID">上一级id,0为一级id</param>
        /// <returns>list集合</returns>
        public List<Category> GetCategory(int UpCID)
        {
            List<Category> list = db.Category.Where(p => p.parent_cate_id == UpCID && p.status == "A").ToList<Category>();
            return list;
        }
        #endregion

//下面写一个删除的 由于我们做的都是修改状态 所以多写一个方法!-_-
        public void DeleteSample()
        {
            db.Category.DeleteOnSubmit(db.Category.Single(c => c.cate_id == 4));
        }

 
分类: LINQ

Linq增删查改