Linq增删改查语法大全

linq语法应用的很多。下面我将linq增删改查的语法介绍给大家,供大家学习和参考。

1.查询语法(分为查询列表和条件查询)

A.查询列表

     public IQueryable<Htel_FinacialPayeeModel> getListInfo() { 
         HotelDataClasses hoteldata=new HotelDataClasses ();
         IQueryable<Htel_FinacialPayeeModel> fianinfo = from h in hoteldata.Htel_FinancialPayee
                                                        select new Htel_FinacialPayeeModel
                                                        {
                                                            Fp_ID = h.Fp_ID,
                                                            Fp_PayeeBank = h.Fp_PayeeBank
                                                        };
         if (fianinfo.Count() > 0)
         {
             return fianinfo;
         }
         else {
             return null;
         }
     }

B.条件查询,返回MOdel数据集

  //编辑信息时通过编号查询到的酒店财务收款列表信息
     public Htel_FinancialPayee GetHtelFinacialInfosByid(long id)
     {
         using (HotelDataClasses hotelData = new HotelDataClasses())
         {
             var info = hotelData.Htel_FinancialPayee.Where(P => P.Fp_ID == id).First();
             return info;

         }
     }

c.多表联合查询语法

有2个表的:

  IQueryable<Htel_FinacialPayeeModel> infolist = from h in hoteldata.Htel_FinancialPayee
                                                            join company in hoteldata.BasCompanyInfo on h.Fp_CompanyId equals company.ID into C
                                                                          where h.Fp_CompanyId == Convert.ToInt32(compnanyid )
                                                                       select new Htel_FinacialPayeeModel
                                                                          {
                                                                              Fp_ID = h.Fp_ID,
                                                                              Companyname = C.First().CompanyName,
                                                                              Fp_Payee = h.Fp_Payee,
                                                                              Fp_PayeeCard = h.Fp_PayeeCard,
                                                                              Fp_PayeeBank = h.Fp_PayeeBank
                                                                  };
             return infolist;

3个以上的表联合查询

   IQueryable<ReservationDetails> info = from inf in db.Htel_OrderInfo.Where(P=>P.OrderCode==OrderCode)
                                                     join dic in db.Htel_Dictionary on inf.ChannelID equals dic.ID into l

                                                     from y in l.DefaultIfEmpty()
                                                     from h in db.Htel_Hotel
                                                     from cn in db.Htel_HotelEN
                                                     from en in db.Htel_HotelCN
                                                     from ddic in db.Htel_Dictionary
                                                     where inf.MemberID == memberid
                                                     where inf.HotelID == h.ID
                                                     where inf.HotelID == cn.HotelID
                                                     where inf.HotelID == en.HotelID
                                                     where inf.PaymentID == ddic.ID
                                                     select new ReservationDetails
                                                     {
                                                         ArrivalTime = inf.ArrivalTime,
                                                         ChannelID = inf.ChannelID,
                                                         ChannelName = y.NameCN,
                                                         CheckIn = inf.Checkin,
                                                         CheckOut = inf.Checkout,
                                                         CompanyID = inf.CompanyID,}

2.修改语法

     public bool Updateinfo(Htel_FinancialPayee model)
     {
         HotelDataClasses hoteldata = new HotelDataClasses();
         var info = hoteldata.Htel_FinancialPayee.Where(p => p.Fp_ID == model.Fp_ID);
         if (info.Count() > 0)
         {
             Htel_FinancialPayee inf = info.First();
             inf.Fp_ID = model.Fp_ID;
             inf.Fp_Payee = model.Fp_Payee;
             inf.Fp_PayeeCard = model.Fp_PayeeCard;
             hoteldata.SubmitChanges();
             return true;

         }
         else
         {
             return false;
         }
     }

3.删除语法
     public bool delInfo(string id) {

         HotelDataClasses hoteldata = new HotelDataClasses();
         var info = hoteldata.Htel_FinancialPayee.Where(p => p.Fp_ID == Convert.ToInt32(id )).First ();
         hoteldata.Htel_FinancialPayee.DeleteOnSubmit(info);
         hoteldata.SubmitChanges();
         return true;
     }

4.增加语法

   public bool Addinfo(Htel_FinancialPayee model) {

         HotelDataClasses hoteldata = new HotelDataClasses();
         Htel_FinancialPayee finamodel = new Htel_FinancialPayee();
         finamodel.Fp_Payee = model.Fp_Payee;
         finamodel.Fp_PayeeBank = model.Fp_PayeeBank;
         hoteldata.Htel_FinancialPayee.InsertOnSubmit(finamodel);
         hoteldata.SubmitChanges();
         return true;
     }

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));
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值