Filter Function

Create and Use Filter Functions

Note: We assume that you have created the data types and added data as described in the demo sample and are going to get Demo.Event items filtered by quarter (Demo.Quarter). Otherwise, customize the code below as you see fit.

In the approach, you will need to create a filter function to be used in the Filter parameter of the standard automatically generated Demo.Event.GetEventXml function.

To create this function:

  1. In the "Function" perspective, select "C# Functions" and click "Add Inline C# Function".
  2. In the window that appears, enter these values:
    • Name: EventsByQuarterFilter
    • Namespace: Demo
    • Template: Method using data connection
  3. On the "Input Parameters" tab, add the parameter:
    • Name: quarterRef
    • Label: Quarter
    • Type: DataReference<Quarter>
  4. On the "Source" tab, copy and paste this code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using Composite.Data;
    using Composite.Data.Types;
    
    namespace Demo
    {
    	public static class InlineMethodFunction
    	{
    		public static Expression<Func<Event,Boolean>> EventsByQuarterFilter(DataReference<Demo.Quarter> quarterRef)
    		{            
    			using (DataConnection connection = new DataConnection())
    			{
    				IEnumerable<Guid> monthIds = 
    					from quarter in connection.Get<Demo.Quarter>()
    					join month in connection.Get<Demo.Month>() on quarter.Id equals month.Quarter
    					where quarter.Id == (Guid)quarterRef.KeyValue
    					select month.Id;
    					
    				return f => monthIds.Contains(f.Month);
    			}            
    		}
    	}
    }
    

    Download the sample code
  5. Save the function.

Now you can use the function in your XSLT. For this:

  1. Edit your XSLT function that should show events filtered by Quarter.
  2. On the "Function Calls" tab, add a function call to Demo.Event.GetEventXml.
  3. For its "Filter" parameter, click "Function" and select Demo.EventsByQuarterFilter.
  4. For the "Quarter" parameter of the filter function, you can create and use an input parameter of the Data Reference <Quarter> type
  5. On the "Template" tab, render the XML returned by your Demo.GetEventXmlByQuarter function, for example, by using <xsl:for-each/>.