Linq 增删改基本操作

 static DataServerDataContext cpt = new DataServerDataContext();
        public List<ProductModel> GetProduct()
        {
         // GetProductDataContext  cpt=new GetProductDataContext();
           var query = from p in cpt.product select p;
           List<ProductModel> products = new List<ProductModel>();
          
           foreach (var item in query)
           {
               products.Add(new ProductModel { Id = Convert.ToInt32(item.Id), Name = item.Name, Count = Convert.ToInt32(item.count )});
           }
           return products  ;
        }
        public void Update(int id,string name,int count)
        {

            //insert
            // product product = new Mvc3.product();
            //  product.Id = id;
            //  cpt.product.InsertOnSubmit(product);

            //update
            var product = cpt.product.Single(p => p.Id == id);
            product.Name = name;
            product.count = count;
            cpt.SubmitChanges();

        }
        public void Delete(int id)
        {

            var product = from p in  cpt.product where p.Id == id select p;
            cpt.product.DeleteAllOnSubmit(product);
            cpt.SubmitChanges();
        }

posted on 2011-09-06 10:35  缘来  阅读(187)  评论(0)    收藏  举报

导航