Delete Record with LINQ

S

shapper

Hello,

I have a table named Tags with the following columns: TagId and
TagValue.
How can I delete a record, using LINQ, given the TagId value.

Thanks,
Miguel
 
M

Michael Nemtsev

just create the var of your record u need to delete
like var myItemToDel = GetRecordToDelete();
and then use .DeleteOnSubmit(myItemToDel)
 
S

shapper

just create the var of your record u need to delete
like var myItemToDel = GetRecordToDelete();
and then use .DeleteOnSubmit(myItemToDel)
--
WBR, Michael Nemtsev [.NET/C# MVP].
Blog:http://spaces.live.com/laflour

shapper said:
I have a table named Tags with the following columns: TagId and
TagValue.
How can I delete a record, using LINQ, given the TagId value.
Thanks,
Miguel

Hi,

I am using the following:

Dim database As New UndefinedDataContext
Dim tag As Tag = From t In database.Tags _
Where t.TagID = New Guid("5f8f46cc-709c-
dc11-8760-000e35bd65c3")
database.Tags.Remove(Tag)

But this is not working. I am using a fixed Guid just for testing.
I am using VB.NET but I understand C# so any suggestion can be in C#.

Thanks,
Miguel
 
M

Marc Gravell

Well, trying something similar on Northwind and using the RTM tools:

Customer cust = ctx.Customers.Where(c => c.CustomerID
== "ALFKI").Single();
ctx.Customers.DeleteOnSubmit(cust);
ctx.SubmitChanges();

Does this not work? Does it error? or do nothing?

Note - the query is equivalent to below - but in this example I find
the Where(...) etc clearer...

Customer cust = (from c in ctx.Customers // your
objects
where c.CustomerID == "ALFKI" // your
guid etc
select c).Single();

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top