Console

The DALINQ console sample demonstrates how to create a DA LINQ query and action that query against a remote schema table 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 (Console), 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.

Included with the sample is a helper1 class which has a single public static method (LogToString) that takes a IList collection object and creates a nicely formatted string representation of the table that can be printed to the Console which can be useful for debugging purposes. (An example of its output can be seen at the end of this document)

Running the Sample

When the sample is run it sets up a connection to the server and creates the LinqRemoteDataAdapter through which the sample will interact with the schema (Step 1). The sample then creates a concrete instance of the Clients object and inserts that row into the schema, before then using DA LINQ to retrieve the new row and print it to the console (Step 2). Next the properties of the Clients object are updated to new values and those changes are applied to the server, before again executing the previous DA LINQ query to show that the values have indeed been updated in the schema (Step 3). Finally the delete row method is called passing in the Clients object and the DA LINQ called one last time to show the row has been deleted from the schema table (Step 4).

Examining the Code

The sample is comprised of 4 classes, though this document focuses on the Program class. It is this class that creates the connection to the schema, creates the DA LINQ query and adds, updates and then deletes 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 IListExtension class (or ListHelper class in the Silver sample) provides a static method (LogToString) that will take an IList collection object retrieved from the DA LINQ query and convert it into a pretty String that will be output to the console.

Using LinqRemoteDataAdapter

Setting up a connection to the server is covered by other samples and won't be touched on here, except to touch upon the use of LinqRemoteDataAdapter rather then RemoteDataAdapter.

The LinqRemoteDataAdapter, like 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.

Adding a row to the table (Step 2)

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 apply those changes to the server you instead use concrete data classes which maps to a row in the table.

After you've created the data object, it needs to be passed to the InsertRow method of LinqRemoteDataAdapter which will contain all of the changes until the ApplyChanges method is called. After that the local object will be updated to have a real ClientId value.

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.

After the query is complete, the sample retrieves a list (IList) of matching rows and passes it to the LogToString method to convert the IList collection to a String object that can be output to the console.

 

Console.WriteLine("STEP 2. Adding a new client row ...");

Clients CurrentClient = new Clients("Jane C.", (Decimal)0.01, "test@gmail.com", "temp", new DateTime(1973, 6, 1), "Unknown yet", "???", "Somewhere in Texas...", "Added row");

lLinqRemoteDataAdapter.InsertRow(CurrentClient);
lLinqRemoteDataAdapter.ApplyChanges();

IQueryable<DALINQ.Simple.Clients> dalinq = from x in lLinqRemoteDataAdapter.GetTable<Clients>()
                                           where x.ClientId == CurrentClient.ClientId
                                           select x;
IList<Clients> lIList = dalinq.ToList();
Console.WriteLine(lIList.LogToString());

 

Console.WriteLine("STEP 2. Adding a new client row ...")

var CurrentClient = Clients("Jane C.", Decimal(0.01), "test@gmail.com", "temp", DateTime(1973, 6, 1), "Unknown yet", "???", "Somewhere in Texas...", "Added row")
lLinqRemoteDataAdapter.InsertRow(CurrentClient)
lLinqRemoteDataAdapter.ApplyChanges()

var dalinq: IQueryable<Clients> = lLinqRemoteDataAdapter.GetTable<Clients>().Where({ x in x.ClientId == CurrentClient.ClientId }).Select({x in x});
var lIList: IList<Clients>! = dalinq.ToList()

Console.WriteLine(listHelper.LogToString(lIList))

Updating a row (Step 3)

Updating an existing row is as simple as updating the properties of a data object and then passing that object to the UpdateRow method of LinqRemoteDataAdapter which will contain all of the changes until the ApplyChanges method is called. After the changes have been applied the local object will be updated to reflect any changes that were waiting on the server.

The previously created DA LINQ query is re-used to demonstrate that the table now contains the updated row data.

 

Console.WriteLine("STEP 3. Updating client row ...");

CurrentClient.ClientName = "Jane Colins";
CurrentClient.ClientDiscount = (Decimal)0.05;
CurrentClient.ClientPhone = "(095) 636-19-63";
CurrentClient.ClientEmail = "janec@gmail.com";
CurrentClient.ClientPassword = "djCkU";
CurrentClient.ClientPostalCode = "83102";
CurrentClient.ClientAddress = "4935 Oliver Street Weatherford, TX 76086";
CurrentClient.ClientNotes = "Updated row";

lLinqRemoteDataAdapter.UpdateRow(CurrentClient);
lLinqRemoteDataAdapter.ApplyChanges();

lIList = dalinq.ToList();

 

Console.WriteLine("STEP 3. Updating client row ...")
CurrentClient.ClientName = "Jane Colins"
CurrentClient.ClientDiscount = Decimal(0.05)
CurrentClient.ClientPhone = "(095) 636-19-63"
CurrentClient.ClientEmail = "janec@gmail.com"
CurrentClient.ClientPassword = "djCkU"
CurrentClient.ClientPostalCode = "83102"
CurrentClient.ClientAddress = "4935 Oliver Street Weatherford, TX 76086"
CurrentClient.ClientNotes = "Updated row"

lLinqRemoteDataAdapter.UpdateRow(CurrentClient)
lLinqRemoteDataAdapter.ApplyChanges()
lIList = dalinq.ToList()

Removing a row (Step 4)

Deleting an existing row can be achieved by passing the row object you wish to delete to the DeleteRow method of LinqRemoteDataAdapter which will contain all of the changes until the ApplyChanges method is called.

The previously created DA LINQ query is re-used to demonstrate that the table now contains the updated row data.

 

Console.WriteLine("STEP 4. Removing client row ...");
lLinqRemoteDataAdapter.DeleteRow(CurrentClient);
lLinqRemoteDataAdapter.ApplyChanges();

 

Console.WriteLine("STEP 4. Removing client row ...")

lLinqRemoteDataAdapter.DeleteRow(CurrentClient)
lLinqRemoteDataAdapter.ApplyChanges()

Example Output

This is an example of the output you will see when the sample is run. As each step is executed the results of the DA LINQ query are printed to the console. So in Step 2 you can see the new row, in Step 3 the details of the row have been updated and finally in Step 4 there is no row to print because it has been deleted from the table.

DA LINQ sample has been started.
Target URL is http://localhost:7099/bin
RO SDK layer is configured.
RO DataAbstract layer is configured.
STEP 1: Login to DataService
User Id="simple";Password="simple";Domain="DASamples";Schema="Simple"
Login has been successful. Going further...

STEP 2. Adding a new client row ...

Table: Clients (1 rows from 1)
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ClientId     | ClientName        | Clie... | ClientEmail       | ClientPassword    | Clie... | ClientAddress     | ClientPostalCode  | ClientPhone       | ClientNotes       |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 27           | Jane C.           | 0.01    | test@gmail.com    | temp              | 01/0... | ...               | ???               | Unknown yet       | Added row         |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|


STEP 3. Updating client row ...

Table: Clients (1 rows from 1)
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ClientId     | ClientName        | Clie... | ClientEmail       | ClientPassword    | Clie... | ClientAddress     | ClientPostalCode  | ClientPhone       | ClientNotes       |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 27           | Jane Colins       | 0.05    | janec@gmail.com   | djCkU             | 01/0... | 4935 Oliver St... | 83102             | (095) 636-19-63   | Updated row       |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|


STEP 4. Removing client row ...
Nothing to log. Rows collection is empty.
DONE! Please press return to exit.


  1. IListExtension in the C# sample, and ListHelper in the Silver/Swift sample.