using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Linq;
namespace DMLLinqinSP
{
class Program
{
private void
Insert()
{
MyEntitiesDataContext
myEntitiesDataContext = new
MyEntitiesDataContext("http://rohit:34143/");
// Get the list from the site
EntityList<ProductsItem> listItems =
myEntitiesDataContext.GetList<ProductsItem>("Products");
//Create a new item
ProductsItem newItem =
new ProductsItem()
{
Title = "Hardware",
ProductID = "5",
ProductName = "RAM"
};
// Insert the new list item to the list
listItems.InsertOnSubmit(newItem);
//Submit the changes
myEntitiesDataContext.SubmitChanges();
Console.WriteLine("Item Inserted");
}
private void
Update()
{
MyEntitiesDataContext
myEntitiesDataContext = new
MyEntitiesDataContext("http://rohit:34143/");
// Querying the list item that has to be
updated
var updateItem = (from item in myEntitiesDataContext.Products where
item.ProductID == "1"
select item).First();
updateItem.ProductID = "6";
updateItem.ProductName = "MotherBoard";
// Submit the changes
myEntitiesDataContext.SubmitChanges();
Console.WriteLine("Item Updated");
}
private void
Delete()
{
// Create an instance
MyEntitiesDataContext
myEntitiesDataContext = new
MyEntitiesDataContext("http://rohit:34143/");
// Get the list from the site
EntityList<ProductsItem> listItems =
myEntitiesDataContext.GetList<ProductsItem>("Products");
// Querying the list item that has to be
deleted
var updateItem = (from item in myEntitiesDataContext.Products where
item.ProductID == "6"
select item).First();
// Deleting the list item
listItems.DeleteOnSubmit(updateItem);
// Submit the changes
myEntitiesDataContext.SubmitChanges();
Console.WriteLine("Item Deleted");
}
static void
Main(string[] args)
{
Program obj =
new Program();
byte ch = 0;
do
{
Console.WriteLine("\t\t\t----Select option----\t\t\t");
Console.WriteLine("\t\t\t----Press (1) For Insert ----\t\t\t");
Console.WriteLine("\t\t\t----Press (2) For Update ----\t\t\t");
Console.WriteLine("\t\t\t----Press (3) For Delete ----\t\t\t");
Console.WriteLine("\t\t\t----Press (0) For Exit ----\t\t\t");
ch = Convert.ToByte(Console.ReadLine());
switch (ch)
{
case 1:
obj.Insert();
break;
case 2:
obj.Update();
break;
case 3:
obj.Delete();
break;
default:
Console.WriteLine("Invalid Option");
break;
}
} while (ch != 0);
}
}
}