views:

319

answers:

1

I'm building an ecommerce site with products being in multiple categories. As a result of this I've got a Products table, a Categories table and a ProdCat linking / junction table that contains all the productId / CategoryId combinations.

When performing an update using Linq To Entities (which I'm using today for the first time) I'm at a loss. I first need to delete all entries for a given product Id and then perform an insert of all the relevant details. I think I have the insert down but can anyone give me an example of how to do the delete?

I've trawled the net and it seems the Linq to Entities is considered the 'way forward' in terms of it versus Linq To SQL but it also seems that it's not as 'easy' to learn as Linq To Sql.

Any help is appreciated.

Thanks.

A: 

You cannot delete in LINQ-to-Anything. Not in LINQ to SQL, not in LINQ to Entities, and not in LINQ to Objects.

To delete in EF, you do:

Context.DeleteObject(someObject);

So to delete all by Id, you'd do:

var q = from entry in Context.SomeEntitySet
        where entry.Product.Id == someId
        select entry;
foreach (var entry in q)
{
    Context.DeleteObject(entry);
}
Context.SaveChanges();
Craig Stuntz