Desktop
The DALINQ console sample demonstrates using a DA LINQ query to retrieve rows from a table in schema as well as the alternative methods for inserting, updating and deleting rows in the schema.
Getting Started
The sample is typically located in C:\Users\Public\Documents\RemObjects Samples\Data Abstract for .NET\<language>\DALINQ
, though you may have installed the Data Abstract for .NET SDK and it's samples in another location.
To build it, you will of course need Visual Studio, and like all the samples provided with Data Abstract for .NET you will need to be running Relativity Server with the DASamples
Domain and the Simple
Schema available.
Running the Sample
The sample provides a simple UI that shows the results of using DA LINQ and the associated LinqRemoteDataAdapter when adding new rows, editing existing rows, deleting selected rows and applying those changes back to the schema table.
When run the sample displays a window that is split into two parts, the first is a toolbar that contains a combo box that contains the server address, and three buttons. The "Load" button will retrieve the table data freshly from the server specified in the combo box.
The "Add" button creates a new Client
and opens an dialog that allows you to edit the details of the new record, in this sample the fields of the dialog are pre-populated. The "Edit" button opens an edit dialog and populates it with the contents of the selected row. The "Delete" button will delete the currently selected row, and finally the "Apply" button will apply the local changes to the server.
Examining the Code
The sample is comprised of 5 classes, though this document focuses solely on the DataModule
class. It is this class that handles the interaction with the Data Abstract server, creates the DA LINQ query and and has the methods to add, update and delete a row from the Clients
table.
The Clients
and Simple_TableDefinitions
classes were generated by the Table Definition Classes
wizard provided by Data Abstract for .NET; the classes handle representing the Clients
table in the Simple
schema and individual clients.
The ClientsForm
class handles the dialog for editing the details of a Client
row. The MainForm
class handles the main user interface, and interacts with the DataModule
to update
Using LinqRemoteDataAdapter
Rather than using the RemoteDataAdapter that other samples use, this sample uses the LinqRemoteDataAdapter class. It, like the RemoteDataAdapter, is a Data Adapter that handles the communication between the client program and the server. Configuration the connection and handling login is identical between the classes, but how they interact with the schema and tables is different. Note that the LinqRemoteDataAdapter adapter does not support Dynamic Where or Dynamic Select per se, but you can use DA LINQ to achieve the same results.
The LinqRemoteDataAdapter also handles tracking the changes to the table data (for instance adding a new row, or deleting an existing one) until the ApplyChanges method is called. At this point all local changes will be applied to the server.
Retrieving records using DA LINQ
The DA LINQ query is built inline, the first part retrieves the table which will be the data source for the remainder of the DA LINQ query. You can then use standard LINQ
queries to work with the data. In this sample, the DA LINQ query selects all the rows and fields in the Clients
table.
After the query is complete, the sample retrieves a List
of the matching rows and passes it to a DataContext
object that is the data source for the table in the user interface.
The SelectClients
method is called from MainForm when the user clicks on the "Load" button.
public void SelectClients()
{
this.linqRemoteDataAdapter.CancelChanges();
IQueryable<Clients> clientsQuery = from c in this.linqRemoteDataAdapter.GetTable<Clients>() select c;
this.Context.Clients = clientsQuery.ToList();
}
method DataModule.SelectClients;
begin
self.linqRemoteDataAdapter.CancelChanges();
var clientsQuery: IQueryable<Clients> := from c in self.linqRemoteDataAdapter.GetTable<Clients>() select c;
self.Context.Clients := clientsQuery.ToList()
end;
public func SelectClients() {
self.linqRemoteDataAdapter.CancelChanges()
var clientsQuery: IQueryable<Clients> = self.linqRemoteDataAdapter.GetTable<Clients>().Select({x in x})
self.Context.Clients = clientsQuery.ToList()
}
Public Sub SelectClients()
Me.linqRemoteDataAdapter.CancelChanges()
Dim clientsQuery As IQueryable(Of Clients) = From c In Me.linqRemoteDataAdapter.GetTable(Of Clients)() Select c
Me.Context.Clients = clientsQuery.ToList()
End Sub
Adding a row to the table
Adding a new row when using DA LINQ is different than when normally using Data Abstract. Instead of adding the row to a DataTable
and using a DataSet
or DeltaChange object to store the data and apply those changes to the server, you instead use concrete data classes which map to a row in the table and pass those the the Data Adapter.
After you've created the data object (here an instance of the Clients
), it needs to be passed to the InsertRow method of LinqRemoteDataAdapter. The LinqRemoteDataAdapter data adapter will track all of the changes to the data and apply them to the server when the ApplyChanges method is called.
The additional line in the code below adds the new row to the DataContext
so that the table will display the new row.
public void AddClient(Clients client)
{
((List<Clients>)this.Context.Clients).Add(client);
this.linqRemoteDataAdapter.InsertRow(client);
}
method DataModule.AddClient(client: Clients);
begin
(List<Clients>(self.Context.Clients)).Add(client);
self.linqRemoteDataAdapter.InsertRow(client)
end;
public func AddClient(client: Clients!) {
(self.Context.Clients as! List<Clients>).Add(client)
self.linqRemoteDataAdapter.InsertRow(client)
}
Public Sub AddClient(client As Clients)
DirectCast(Me.Context.Clients, List(Of Clients)).Add(client)
Me.linqRemoteDataAdapter.InsertRow(client)
End Sub
Updating a row
Updating an existing row is as simple as updating the properties of the data object representing the row and then passing that object to the UpdateRow method of LinqRemoteDataAdapter.
The LinqRemoteDataAdapter data adapter will track all of the changes to the data and apply them to the server when the ApplyChanges method is called.
public void UpdateClient(Clients client)
{
this.linqRemoteDataAdapter.UpdateRow(client);
}
method DataModule.UpdateClient(client: Clients);
begin
self.linqRemoteDataAdapter.UpdateRow(client)
end;
public func UpdateClient(client: Clients!) {
self.linqRemoteDataAdapter.UpdateRow(client)
}
Public Sub UpdateClient(client As Clients)
Me.linqRemoteDataAdapter.UpdateRow(client)
End Sub
Removing a row
To delete an existing row pass the data object that represents the row you wish to delete to the DeleteRow method of LinqRemoteDataAdapter.
The LinqRemoteDataAdapter data adapter will track all of the changes to the data and apply them to the server when the ApplyChanges method is called.
The additional line in the code below removes the row from the DataContext
so that the table will no longer show the row.
public void DeleteClient(Clients client)
{
this.linqRemoteDataAdapter.DeleteRow(client);
((List<Clients>)this.Context.Clients).Remove(client);
}
method DataModule.DeleteClient(client: Clients);
begin
self.linqRemoteDataAdapter.DeleteRow(client);
(List<Clients>(self.Context.Clients)).Remove(client)
end;
public func DeleteClient(client: Clients!) {
self.linqRemoteDataAdapter.DeleteRow(client)
(self.Context.Clients as! List<Clients>).Remove(client)
}
Public Sub DeleteClient(client As Clients)
Me.linqRemoteDataAdapter.DeleteRow(client)
DirectCast(Me.Context.Clients, List(Of Clients)).Remove(client)
End Sub