4

Question: what is the LINQ-to-Entity code to insert an order for a specific customer?

enter image description here

Update

Here is the a solution (see one of the submitted answers below for a much cleaner solution):

using (OrderDatabase ctx = new OrderDatabase())
{
  // Populate the individual tables.

  // Comment in this next line to create a new customer/order combination.
  // Customer customer = new Customer() { FirstName = "Bobby", LastName = "Davro" }; 
  // Comment in this line to add an order to an existing customer.
  var customer = ctx.Customers.Where(c => c.FirstName == "Bobby").FirstOrDefault(); 

  Order order = new Order() { OrderQuantity = "2", OrderDescription = "Widgets" }; 

  // Insert the individual tables correctly into the hierarchy.
  customer.Orders.Add(order);

  // Add the complete object into the entity.
  ctx.Customers.AddObject(customer);

  // Insert into the database.
  ctx.SaveChanges();                        
}
Contango
  • 76,540
  • 58
  • 260
  • 305
  • Of course, I could probably use ADO.NET to do the job - but I don't want to "pollute" my model by dealing with primary/foreign keys. Notice that the code above doesn't have primary/foreign keys - LINQ to Entities handles this for me. – Contango Feb 07 '11 at 21:09

4 Answers4

3

Your code isn't far off. Just change your second line to read as follows:

Customer customer = ctx.Customer.FirstOrDefault(c => c.FirstName == "Bobby");
if (customer != null)
{
    //...

Just replace the c.FirstName == "Bobby" with something that can strongly identify the customer you're looking for (e.g. c.Id == customerID if you already know what the ID is).

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
  • 1
    Brilliant, this works nicely. I had to award the answer to you as you answered correctly first. Many, many thanks! – Contango Feb 07 '11 at 21:53
3

Notice that Order has a Customer property. You don't have to add the Order to the Customer -- you can do it the other way around. So, instead of creating a new Customer, get the Customer using Linq, then add it to your new Order.

using (OrderDatabase ctx = new OrderDatabase())
{
    ctx.AddOrder(new Order()
    {
        OrderQuantity = 2,
        OrderDescription = "Widgets",
        Customer = ctx.Customers.First<Customer>(c => c.CustomerId == yourId)
    });
    ctx.SaveChanges();
}
Andrew
  • 14,325
  • 4
  • 43
  • 64
  • 1
    Note that you can replace the condition to select the Customer with any other relevant condition; you don't need yourId to have the CustomerId if you want to search on other criteria. – Andrew Feb 07 '11 at 21:14
1

I don't get what the problem is, exactly.

var mycustomer = context.Customers.Where(x => x.id == 100).FirstOrDefault();
if(mycustomer != null)
{
  mycustomer.Orders.Add(myorder); 
}
context.SaveChanges();
Jcl
  • 27,696
  • 5
  • 61
  • 92
0

L2E does not support set-based operations currently (update without select). See Use linq to generate direct update without select

Community
  • 1
  • 1
esac
  • 24,099
  • 38
  • 122
  • 179