RelationCollectionAdd(Entity, RelationType, FilterBase, Sort) Method

Adds a relation toe this collection

Definition

Namespace: Firefly.Box.Advanced
Assembly: Firefly.Box (in Firefly.Box.dll) Version: debug-master-v:33791
C#
public Relation Add(
	Entity entity,
	RelationType type,
	FilterBase where,
	Sort orderBy
)

Parameters

entity  Entity
The entity from which to extract the related row
type  RelationType
The type of the relation
where  FilterBase
The filter that will be used to find the related row
orderBy  Sort
The order in which the rows will be fetched

Return Value

Relation

Example

This example demonstrates the usage of FetchingRowsWithRelations
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 System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Firefly.Box;
using Firefly.Box.Testing;

namespace TestFirefly.Box.Documentation
{
    [TestFixture]
    public class FetchingRowsWithRelations
    {
        [Test]
        public void UsingRelationForALookupTable()
        {
            var employees = new Pubs.Employees();
            var jobs = new Pubs.Jobs();
            employees.InitializeWithTestData();
            jobs.InitializeWithTestData();

            var bp = new BusinessProcess
            {
                From = employees
            };
            bp.Relations.Add(jobs, jobs.Id.IsEqualTo(employees.JobId));
            string namesAndJobs = "";
            bp.ForEachRow(() =>
            {
                namesAndJobs += employees.LastName.Value.TrimEnd() + " - " +
                                jobs.Description.Value.TrimEnd() + "\n";
                if (bp.Counter == 5)
                    bp.Exit();
            });
            namesAndJobs.ShouldBe("Cruz - Productions Manager\n" +
                "Devon - Business Operations Manager\n" +
                "Roulet - Managing Editor\n" +
                "Domingues - Public Relations Manager\n" +
                "Hernadez - Publisher\n");
        }
        [Test]
        public void UsingRelationToFetchTheTopMostRow()
        {
            var jobs = new Pubs.Jobs();
            var employees = new Pubs.Employees();
            jobs.InitializeWithTestData();
            employees.InitializeWithTestData();

            var bp = new BusinessProcess()
            {
                From = jobs
            };
            //A relation that gets the employee with the highest joblevel in the matching job
            bp.Relations.Add(employees, employees.JobId.IsEqualTo(jobs.Id),
                new Sort(employees.JobLevel)).OrderBy.Reversed = true;

            string jobsAndEmployeesWithMaxLevel = "";

            bp.ForEachRow(() =>
            {
                jobsAndEmployeesWithMaxLevel += jobs.Description.ToString().TrimEnd() + " - " +
                                                employees.LastName.ToString().TrimEnd() + "\n";
            });
            jobsAndEmployeesWithMaxLevel.ShouldBe(
                "New Hire - Job not specified - \n" +
                "Chief Executive Officer - Cramer\n" +
                "Business Operations Manager - Devon\n" +
                "Chief Financial Officier - Chang\n" +
                "Publisher - Pontes\n" +
                "Managing Editor - Karttunen\n" +
                "Marketing Manager - Ibsen\n" +
                "Public Relations Manager - Saveley\n" +
                "Acquisitions Manager - Jablonski\n" +
                "Productions Manager - Sommer\n" +
                "Operations Manager - Mendel\n" +
                "Editor - Snyder\n" +
                "Sales Representative - O'Rourke\n" +
                "Designer - Josephs\n");
        }

    }
}

See Also