CalcEngine supports data binding and uses it's default engine, i.e., VBA Engine, to bind to a collection. This collection can either be an array or a list. The example of data binding using an array is already demonstrated in the Quick Start topic. In the following examples, we use a list to demonstrate data binding wherein we showcase data binding with a collection and its subset.
The following example demonstrates binding the collection to the engine using DataSource property of the C1CalcEngine class and aggregate function to work with multiple values and evaluate the result.
C# |
コードのコピー
|
---|---|
public class Customer { public int Id { get; set; } public string Name { get; set; } } var collection = new List<Customer>() { new Customer { Id = 27, Name = "Den" }, new Customer { Id = 15, Name = "Anna" } }; // コレクションおよび集計関数を使用します。 var engine = new VBAEngine(); engine.DataSource = collection; engine.Expression = "Sum([Id])"; Assert.AreEqual(42, engine.Evaluate()); |
The following example demonstrates binding the first record of the collection with the engine using the DataSource property to operate on a single data field and then using an expression to evaluate the result. This example uses the Customer class created in the Data Binding to a Collection section.
C# |
コードのコピー
|
---|---|
// 単一のデータエントリによる連結します。 var engine = new VBAEngine(); engine.DataSource = collection[0]; engine.Expression = "[Id] + 1"; Assert.AreEqual(28, engine.Evaluate()); |
In the following example, we demonstrates binding the collection to the engine using the DataSource property and setting the CurrentIndex property to evaluate the expression for a specific entry.
C# |
コードのコピー
|
---|---|
// コレクションを操作して単一のエントリを評価します。 var engine = new VBAEngine(); engine.DataSource = collection; engine.CurrentIndex = 1; engine.Expression = "[Id] / Sum([Id])"; Assert.AreEqual(0.380952380952381, engine.Evaluate()); |