How to break out of a foreach loop?

The Template Syntax page explains using a foreach loop, which I have done successfully. However, sometimes when I find a match in the loop, I want to break out of the loop and move on to the next part of the code. In C#, you can use the break or return statement to do this, but that doesn’t seem to work for me. I’ve tried <<break>>, <<[break]>>, etc. but it doesn’t seem to have an effect. Is this possible?

Also, is there another more definitive syntax reference available besides the Template Syntax page? That seems to have incomplete information so it’s hard to know which C# statements and methods are available and which aren’t.

Thanks.

@bartc,

We are checking this scenario and will get back to you soon.

@bartc,

I am afraid, there is no break tag in LINQ Reporting Engine template syntax. However, your requirements can be achieved in several other ways.

Option 1

You can use one of the supported LINQ extension methods (Where/Skip/SkipWhile/Take/TakeWhile) to filter out enumeration items that should not be processed as follows:

<<foreach [in users.Where(u => u.IsActive)]>>…<</foreach>>

Option 2

You can use a foreach tag in conjunction with an if tag to do the same as follows:

<<foreach [in users]>><<if [IsActive]>>…<</if>><</foreach>>

Option 3

If you need only to calculate a single value, then there is no need to use a foreach tag at all as shown in the following template examples:

<<[users.FirstOrDefault(u => u.IsActive)?.Name]>>

Or

<<var [name = users.FirstOrDefault(u => u.IsActive)?.Name]>>
// Do something with the variable.

Our template syntax reference is complete. We do not support features beyond that. Also, there is no mention about that we support C# statements (foreach in our template syntax is a tag, not a C# statement). What we do support is a subset of C# expression syntax. Here is a relevant quote form our documentation: “While composing expressions, you can use a subset of C# language that satisfies C# Language Specification 5.0.” See here:

LINQ Reporting Engine Features

All supported features are listed in our template syntax reference. Hope that helps.

Thank you, that is extremely helpful!