BusinessProcessForEachRow(Action) Method

Performs the specified action on each row that matches the Where property filter.

Definition

Namespace: Firefly.Box
Assembly: Firefly.Box (in Firefly.Box.dll) Version: debug-master-v:33791
C#
public long ForEachRow(
	Action action
)

Parameters

action  Action
The Action to be performed for each row

Return Value

Int64

Remarks

Created for ease of use scenarios.
Most simple business operations needs to perform an action for each row that matches the filter.
Behaves the same way as registering that action to the LeaveRow event, and invoking the Run method.

Example

This example demonstrates both ForEachRow and ForFirstRow
This example is in the form of Unit Tests. It references the NUnit framework. This framework can be downloaded from www.NUnit.org. For more information about unit testing visit: www.NUnit.org.
This example is based on test data. The code for the entities included in this test data can be found in the documentation of Entity
C#
using NUnit.Framework;
using Firefly.Box;
using Firefly.Box.Testing;

namespace TestFirefly.Box.Documentation
{
    [TestFixture]
    public class ForEachRow
    {
        [Test]
        public void SumOfMinLevel()
        {
            var jobs = new Pubs.Jobs();
            jobs.InitializeWithTestData();

            int totalMinLevel = 0;

            var bp = new BusinessProcess
                         {
                             From = jobs
                         };
            bp.ForEachRow(() => totalMinLevel += jobs.MinLevel);
            totalMinLevel.ShouldBe(1370);
        }


        [Test]
        public void ForEachRowDemo()
        {
            var jobs = new Pubs.Jobs();
            jobs.InitializeWithTestData();
            string jobTitles = "";

            var bp = new BusinessProcess
                         {
                             From = jobs,
                         };
            bp.Where.Add(jobs.Id.IsLessOrEqualTo(3));
            bp.ForEachRow(() =>
                              {
                                  if (jobTitles.Length > 0)
                                      jobTitles += ", ";
                                  jobTitles += jobs.Description.Value.TrimEnd();
                              });

            jobTitles.ShouldBe(
                "New Hire - Job not specified, Chief Executive Officer, Business Operations Manager"
                );
        }
        [Test]
        public void AchievingTheBehaviourOfForEachRowWithLeaveRow()
        {
            var jobs = new Pubs.Jobs();
            jobs.InitializeWithTestData();
            string jobTitles = "";

            var bp = new BusinessProcess
            {
                From = jobs,
            };
            bp.Where.Add(jobs.Id.IsLessOrEqualTo(3));
            bp.LeaveRow += () =>
            {
                if (jobTitles.Length > 0)
                    jobTitles += ", ";
                jobTitles += jobs.Description.Value.TrimEnd();
            };
            bp.Run();

            jobTitles.ShouldBe(
                "New Hire - Job not specified, Chief Executive Officer, Business Operations Manager"
                );
        }
        [Test]
        public void ForFirstRowDemo()
        {
            var jobs = new Pubs.Jobs();
            jobs.InitializeWithTestData();
            string jobTitles = "";

            var bp = new BusinessProcess
            {
                From = jobs,
            };
            bp.Where.Add(jobs.Id.IsLessOrEqualTo(3));
            bp.ForFirstRow(() =>
            {
                if (jobTitles.Length > 0)
                    jobTitles+=", ";
                jobTitles+=jobs.Description.Value.TrimEnd();
            });

            jobTitles.ShouldBe("New Hire - Job not specified");
        }
        [Test]
        public void AchievingTheBehaviourOfForFirstRowWithLeaveRowEvent()
        {
            var jobs = new Pubs.Jobs();
            jobs.InitializeWithTestData();
            string jobTitles = "";

            var bp = new BusinessProcess
            {
                From = jobs,
            };
            bp.Where.Add(jobs.Id.IsLessOrEqualTo(3));
            bp.LeaveRow += () =>
            {
                if (jobTitles.Length > 0)
                    jobTitles+=", ";
                jobTitles+=jobs.Description.Value.TrimEnd();
            };
            bp.Exit(ExitTiming.AfterRow);
            bp.Run();

            jobTitles.ShouldBe("New Hire - Job not specified");
        }
    }
}

See Also