Products Technologies Demo Docs Blog Support Company

LINQ to DataSet: Filter Your Data Before Merging

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…

LINQ to DataSet: Filter Your Data Before Merging

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<DataRow> query =
    from contact in dt.AsEnumerable()
    where contact.Field<String>("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<DataRow>();

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<DataRow> query =
    from contact in dt.AsEnumerable()
    where contact.Field<String>("company") == "Microsoft"
    orderby contact.Field<String>("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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Reporting

The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.

See Reporting products

Related Posts

ASP.NETReportingHTML5

Creating Your First ASP.NET Reporting Application

This tutorial shows how to use the MailMerge component in an ASP.NET Web application to merge a template with data to create an Adobe PDF document.


ASP.NETReportingTutorial

New Online Sample: Build your First Report

We published a new online demo that shows how to create a report including preparing data, creating a template to merging them together.


ReportingDocumentationReportingCloud

Create your First Document with ReportingCloud

As part of our new ReportingCloud documentation, we published a guided tutorial that shows how to create a document without programming.


CloudReportingMail Merge

MailMerge: Starting Each Merge Block on a New Page

A merge block is repeated based on the number of matching data rows in the hierarchical data object. The complete merge block is cloned and inserted under the original location in the template.…


ReportingTutorialWeb API

Using MailMerge with JSON Data

In the last article, we explained how to create an ASP.NET Web API to merge templates with JSON data in the payload body of an HTTP request. The focus of this article was on the Web API and…