您的位置:首页 > 其它

Linq增删改查语法大全

2011-09-14 00:43 281 查看
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;

     }

 

 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息