It is parallel in use and function to the System.Data.Linq.DataContext class in the LINQ to SQL provider.
Just as the latter class has a GetTable() method that returns a Table<TEntity> object that implements System.Linq.IQueryable<T>, so too, the Microsoft.SharePoint.Linq.DataContext class has a GetList<T>method that returns an EntityList<TEntity> class that implements System.Linq.IQueryable<T>.
It is objects of type EntityList<TEntity> that are queried.
The following is an example of the use of LINQ to query SharePoint Foundation.
// Get DataContext from page context DataContext data = new DataContext(SPContext.Current.Web.Url); // Get the SharePoint list EntityList<Customer> Customers = data.GetList<Customer>("Customers"); // Query for customers from London var londonCustomers = from customer in Customers where customer.City == "London" select customer; foreach (var londonCust in londonCustomers) { Console.Writeline("id = {0}, City = {1}", londonCust.CustomerId, londonCust.City); }
The following is an example of using LINQ to add an item to a SharePoint Foundation list.// Get DataContext from page context DataContext data = new DataContext(SPContext.Current.Web.Url); // Get the SharePoint list EntityList<Customer> Customers = data.GetList<Customer>("Customers"); // Create the item to be added Customer newCustomer = new Customer() { CustomerId=36, City=”Madrid” }; // Mark the item to be added on the next call of Submit Customers.InsertOnSubmit(newCustomer); // Submit all changes data.SubmitChanges();
No comments:
Post a Comment