About sum function using in aspose word template in LINQ report engine

Hi Team,

How to add sum () or convert below expression in aspose word template using LINQ report engine.

=CInt(Sum(Fields!NominalFrequency.Value)).ToString()

@rose.thomas

How to calculate a sum in an Aspose.Words .NET template with the LINQ Reporting Engine

  1. Use the built‑in Sum extension method
    The LINQ Reporting Engine provides extension methods that mimic the standard LINQ operators (e.g., Sum, Count, Average). These methods can be called directly inside a template expression [4].

  2. Template syntax
    Replace the SSRS‑style expression

    =CInt(Sum(Fields!NominalFrequency.Value)).ToString()
    

    with a LINQ Reporting Engine expression that uses the Sum method, for example

    =Fields!NominalFrequency.Sum().ToString()
    // or
    =Sum(Fields!NominalFrequency).ToString()
    
    • Fields!NominalFrequency refers to the collection element that holds the numeric values.
    • Sum() aggregates all items of that collection.
    • ToString() renders the result in the document.
      (The exact syntax follows the LINQ Reporting Engine template rules [4].)
  3. Data source requirements
    The data source passed to ReportingEngine.BuildReport must expose a collection (XML, JSON, CSV, or a .NET object) whose items contain a member named NominalFrequency. The engine evaluates the Sum expression against that collection at runtime [2].

  4. Typical usage in code

    var engine = new ReportingEngine();
    engine.BuildReport(templateDoc, dataSource, "MyData");
    

    dataSource can be an XmlDataSource, JsonDataSource, CsvDataSource, or a .NET object list that includes the NominalFrequency property [2].

Summary – Use the Sum extension method provided by the LINQ Reporting Engine inside the template (e.g., =Fields!NominalFrequency.Sum().ToString()). Ensure the data source contains the NominalFrequency field so the engine can perform the aggregation.

to the below mentioned field I need to do sum() function.

<<[ds[0].NominalFrequency)]>>

@rose.thomas

How to calculate a sum in an Aspose.Words template with the LINQ Reporting Engine

  1. Use the built‑in Sum extension method – the engine provides LINQ‑style aggregation methods ( Sum, Count, Average ) that can be called directly inside a template expression [1].

  2. Replace the SSRS‑style expression

    =CInt(Sum(Fields!NominalFrequency.Value)).ToString()
    

    with one of the following LINQ Reporting Engine forms:

    =Fields!NominalFrequency.Sum().ToString()
    // or
    =Sum(Fields!NominalFrequency).ToString()
    
    • Fields!NominalFrequency must refer to a collection element that holds the numeric values.
    • Sum() aggregates all items of that collection at runtime.
    • ToString() renders the result in the document [1].
  3. Ensure the data source exposes the field – the object (XML, JSON, CSV, or a .NET list) passed to ReportingEngine.BuildReport must contain a member named NominalFrequency for each item in the collection [1][5].

  4. Typical code to build the report

    var engine = new ReportingEngine();
    engine.BuildReport(templateDoc, dataSource, "MyData");   // dataSource contains NominalFrequency
    

    The dataSource can be an XmlDataSource, JsonDataSource, CsvDataSource, or a .NET object list [1][5].

  5. Optional – use a variable for a total row
    If you need a separate total row in a table, define a variable in the template (e.g., {{totalNominalFrequency}}) and assign it the sum expression as shown above [2].

Why it works – The LINQ Reporting Engine evaluates the Sum extension method against the enumerable that backs Fields!NominalFrequency, mimicking the standard LINQ Enumerable.Sum behavior [3].

While I am using sum() as mentioned above getting below screenshot error. Please assist.

Template:

coverTemplateISO17025.docx (31.1 KB)

@rose.thomas You can use standard LINQ syntax. For example:

Product[] products = new Product[]
{
    new Product { Name = "Laptop", Price = 999.99 },
    new Product { Name = "Mouse", Price = 19.99 },
    new Product { Name = "Keyboard", Price = 45.50 }
};

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<[products.Sum(p => p.Price)]>>");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, products, "products");

doc.Save(@"C:\Temp\out.docx");
public class Product
{
    public string Name { get; set; }
    public double Price { get; set; }
}

Thanks for your updates, Is there any way to add sum in aspose word template instead in c# code?

@rose.thomas Yes, you can use the following syntax in your template, just like shown above (template simply created via code in the provided example):

<<[products.Sum(p => p.Price)]>>

Hi, I have tried similar way but getting below mentioned error.

used expression in template as below. Anything wrong in my expression?
<<[ds[0].Sum(p => p.NominalFrequency)]>> Hz

Report frequency field as getting as below if I am not using sum().
Required as below:

without sum() in aspose word report:

@rose.thomas Sum is supposed to be used with enumerable types. Most likely your ds[0] is not enumerable, that is why the exception is thrown.

Regarding value formatting, please see our documentation to learn how to format values:
https://docs.aspose.com/words/net/outputting-expression-results/

Hi,

How to convert the below code into aspose word template?

=CInt(Sum(ds[0].NominalFrequency.Value)).ToString()
both string and Int conversion need to apply

@rose.thomas What is the type of ds[0].NominalFrequency.Value? In LINQ reporting syntax you can use standard C# syntax.

Hi,
type of ds[0].NominalFrequency.Value is decimal(18,10).

@rose.thomas Thank you for additional information. Sum function is supposed to have at least two parameters to sum them. If the value is simple decimal, I do not see any sense of using Sum function.

Hi,

Can we able to round of the field as mentioned below.
Frequency value is showing as 50.000000 HZ
Need : 50 Hz
Required
image.png (10.6 KB)

Current output:
image.png (77.6 KB)

@rose.thomas Please see our documentation to learn how to format the values:
https://docs.aspose.com/words/net/outputting-expression-results/

For example you can use the following syntax:

<<[value]:"#.00">>