I searched far and wide (well, more than four Google searches) for a solution to this and in the end I eventually gleaned (above) that LINQ doesn't do deletes - so you use LINQ to select the rows you want to get rid of and delete them through the 'usual' mechanism of the database technology in use. Seems to make all that database-agnostic propaganda about LINQ pretty silly? This one does at least work although I will play around with the two 'For Each' loops and see if I can reduce that to one.
I had a table of Properties where I wanted to delete all the entries with a chosen property name (third keyfield) for an object type (first keyfield) and I came up with this. FYI the REPLY object is a bit like the Error object - I just package error messages, a binary Pass/fail flag and a few other things so I can pass back loads of stuff from a query and (eventually) show the error to the user.
' <summary>
' The user has decided they don't want a particular property type so we delete all
' properties of that type in the table - belonging to any object of that ObjectType
' </summary>
' <param name="sObjectType"></param>
' <param name="sName"></param>
' <returns></returns>
' <remarks></remarks>
Public Function DeleteAllPropsByName(sObjectType As String, sName As String) As Reply
DeleteAllPropsByName = New Reply(True)
Dim T As DataTable = mDB.DataTableImage(msTableName)
Dim q = From rw In T.AsEnumerable
Where rw.Field(Of String)("ObjectType") = sObjectType _
And rw.Field(Of String)("PropName") = sName
' Somewhere to remember our list of target rows to delete
Dim rows As New List(Of DataRow)
For Each row In q
'
' LINQ doesn't delete so we need to delete from the Datatable directly.
' If we delete here we get the collection modified error so we have to save
' the datarows to ANOTHER list and then delete them from there.
rows.Add(row)
Next
For Each rw As DataRow In rows
'
' Call the Delete routine in my table class passing it the
' primary key of the row to delete
'
DeleteAllPropsByName = gDB.Table(msTableName).Delete( _
rw.Item("ObjectType").ToString, _
CInt(rw.Item("ObjectID")), _
rw.Item("PropName").ToString)
If Not DeleteAllPropsByName.OK Then
' The reply object (in DeleteAllPropsByName) has all the error info so we can just
' exit and return it if the delete failed.
Exit Function
End If
Next
End Function
DataRow
instead? Apart from that, what means "not retrievable"? – Tim SchmelterFrom cust In custTable.AsEnumerable Where cust.Field(Of String)("Ccd").Contains(txtCustID.Text)
and returns all datarows(cust
) where theCcd
field contains the text in the textbox. – Tim SchmelterIEnumerable(Of DataRow)
that you can enumerate in aFor Each
or which you can use to create another collection(f.e. withCopyToDataTable
aDataTable
or withToList
aList(Of DataRow)
). Then you can access each column via columnname or ordinal index. So actually you have all columns. If you need all columns in a single property, that's a different question. Maybe you want to join all columns to a singleString
. Please clarify your requirement. – Tim Schmelter