Linq. Delete Record

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have two tables:

[Tags] > TagId, PostId
[PostsTags] > TagId, PostId

How can I delete, given a TagId, the record from Tags and all records
associated with it in PostTags.

I have created the Linq to SQL classes.

I need to create this code inside my LinqDataSource, which I use as
DataSource of my ListView:

private void lds_Deleting(object sender, LinqDataSourceDeleteEventArgs
e)
{
}

Could someone, please, help me out?

Thanks,
Miguel
 
Miguel,

Do you have a need to do this using Linq to SQL? To be quite honest, a
simple stored procedure (or even two parameterized queries) would suffice:

delete from PostsTags where TagId = ?
delete from Tags where TagId = ?

Of course, you haven't specified the relationship between the two
tables, so the above statements could be wrong.

However, I would say that using the LINQ to SQL classes in this specific
instance (where you have a very specific operation) is a little overboard.

Or, if you must use LINQ to SQL and have set up the relations between
the classes correctly, you could just call Delete on the LinqDataSource
instance.
 
To elaborate, in your event, you would call Delete on the associated
records. The thing is, if you have the relations set up correctly, you
^shouldn't^ have to do this.
 
Back
Top