The DocumentServer.MailMerge class is a powerful reporting engine to create MS Word compatible flow type layout reports. MailMerge is matching data source columns with merge fields and nested relations automatically. The supported data source structure is DataSet and DataTable.

Speaking of data structures: For the next version X10 (20.0), we are working on support for other data structures like business objects with implicit data relations. But for now, a DataTable is a very powerful construct and since .NET 3.5, you are getting some extensive new features.

The DataSet API has been extented with the DataSetExtensions. They allow you to use Language-Integrated Query (LINQ) expressions against DataTable objects.

The following code creates a DataTable with 3 columns and 4 rows with contact information:

DataTable dt = new DataTable();

dt.Columns.Add("company");
dt.Columns.Add("firstname");
dt.Columns.Add("name");

dt.Rows.Add(new object[] { "Facebook", "Mark", "Zuckerberg" });
dt.Rows.Add(new object[] { "Microsoft", "Bill", "Gates" });
dt.Rows.Add(new object[] { "Microsoft", "Steve", "Ballmer" });
dt.Rows.Add(new object[] { "Oracle", "Larry", "Ellison" });
LINQ to DataSet

A LINQ query on the DataTable is used to select all contacts where company equals Microsoft. This is possible after the DataTable is converted into an enumerable object using the extensions.

IEnumerable query =
    from contact in dt.AsEnumerable()
    where contact.Field("company") == "Microsoft"
    select contact;
LINQ to DataSet

Finally, the DataRows are copied into a new DataTable using the extension CopyToDataTable:

// create a table from the query
DataTable boundTable = query.CopyToDataTable();

You can do much more with LINQ in the query. Let's say we want to order the results alphabetically by name. The keyword orderby can be used to define the desired order:

IEnumerable query =
    from contact in dt.AsEnumerable()
    where contact.Field("company") == "Microsoft"
    orderby contact.Field("name")
    select contact;
LINQ to DataSet

This new DataTable object can be used in the Merge method of the TX Text Control MailMerge class to pass the merge data.