Using LINQ in complex scenarios in SharpShooter Reports

Dmitry Plutalov

In some cases it is convenient to perform some manipulations with data in code of report scripts. And if you use a framework that supports LINQ you can also use it in report scripts.
 

To do this, you should add the following lines into the list of the assemblies you use (add them as two lines without separators in the SharpShooter Reports Designer, the Document tab, the Script group, the «Imports Namespaces» button):
 

System.Data
System.Linq

Example:

Let’s assume we have a list of lines that includes the “One”, “Two”, “Three”, “Four”, “Five” values. We want to display all lines except “One” and sort by the line length. It can be easily done using the following code:
 

First, we need to get original data source:
 

System.Collections.Generic.List<string> data = DataObjects["data"] as System.Collections.Generic.List<string>;

Now we need to filter it and sort
 

var sortedData = data.Where(t => t != "One").OrderBy(t => t.Length).ToList();

Finally, we need to add it to the data sources collection and set it as data band’s data source name.
 

dataBand1.DataSource = "orderedData";
DataObjects.Remove("orderedData");
DataObjects.Add("orderedData", sortedData);

Please note that this example also demonstrates how to register a newly-created object in data sources collection.

September 26th, 2012

Leave a Comment