using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Query;

namespace _3_ReverseMap
{
  class Program
  {
    static void Main(string[] args)
    {
      using (IObjectScope scope = ObjectScopeProvider1.ObjectScope())
      {
        scope.Transaction.Begin();

        // add a new customer
        Customer newCustomer = new Customer();
        newCustomer.CustomerID = "A" + System.DateTime.Now.ToString("hmsfff");
        newCustomer.CompanyName = "New Customer";
        scope.Add(newCustomer);

        // get first customer using LINQ and modify
        Customer firstCustomer = scope.Extent<Customer>().Take(1).Single();
        firstCustomer.CompanyName = "Company " + DateTime.Now.ToString("hmsfff");
        scope.Add(firstCustomer);

        // use LINQ to retrieve customer ID's starting with "E"
        var results =
          from c in scope.Extent<Customer>()
          where c.CompanyName.StartsWith("E")
          select c;

        // delete "E" customers from the database
        foreach (Customer cust in results)
        {
          foreach (Order order in cust.Orders)
          {
            foreach (OrderDetail detail in order.OrderDetails)
            {
              scope.Remove(detail);
            }
            scope.Remove(order);
          }
          scope.Remove(cust);
        }

        scope.Transaction.Commit();
      }

      Console.Write("Done");
      Console.ReadLine();
    }
  }
}
