BusinessProcessForFirstRow(FilterBase, Action) Method

Performs the specified action on the first 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 bool ForFirstRow(
	FilterBase where,
	Action action
)

Parameters

where  FilterBase
The Action to be performed for the first row
action  Action
 

Return Value

Boolean

Remarks

Usually used to insert a single row to an entity, or fetching the value of a certain row
Behaves the same way as registering that action to the LeaveRow event, and calling the Exit(ExitTiming, FuncBoolean) method while specifying the AfterRow timing, 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