Sharepoint学习笔记---Linq to Sharepoint--增,删,改操作

首先在我们的测试网站创建一个名为MyProducts的List,定义三个Customer Column均为String类型。如图

当然,你不用插入任何记录。
下面以此List为对象进行相应的增,删,改操作

1、添加操作

            var dc = new NorthWindEntityDataContext(SPContext.Current.Web.Url);
            EntityList<MyProductsItem> MyCustProducts;
            MyCustProducts = dc.GetList<MyProductsItem>("MyProducts");
            string ProductNameStr = "NewProductName";
            MyProductsItem newProduct = new MyProductsItem();
            newProduct.Title = "ATestPrd" + DateTime.Now.ToShortTimeString().Trim();
            newProduct.ProductName = ProductNameStr;
            newProduct.ProductPrice = "15";
            dc.MyProducts.InsertOnSubmit(newProduct);
            dc.SubmitChanges();  

 

2、修改操作

            var dc = new NorthWindEntityDataContext(SPContext.Current.Web.Url);
            EntityList<MyProductsItem> MyCustProducts;
            string ProductNameStr = "ModifiedProductName";
            MyCustProducts = dc.GetList<MyProductsItem>("MyProducts");
            var updateItem = (from p in MyCustProducts
                              where p.ProductName == ProductNameStr
                              select p).First();
            updateItem.Title = "Updated" + ProductNameStr;

            // Submit the changes
            dc.SubmitChanges();

 

3、删除操作

            var dc = new NorthWindEntityDataContext(SPContext.Current.Web.Url);
            EntityList<MyProductsItem> MyCustProducts;
            MyCustProducts = dc.GetList<MyProductsItem>("MyProducts");
            string ProductNameStr = this.txtBxProductName.Text.ToString();
            // Querying the list item that has to be deleted
            var delItem = (from p in MyCustProducts
                           where p.ProductName == ProductNameStr
                              select p).First();

            // Deleting the list item
            MyCustProducts.DeleteOnSubmit(delItem);

            // Submit the changes            
            dc.SubmitChanges();   

 

posted @ 2011-11-06 14:09  wsdj  阅读(1703)  评论(0编辑  收藏  举报