Skip to content

Instantly share code, notes, and snippets.

@idusortus
Last active December 4, 2025 13:57
Show Gist options
  • Select an option

  • Save idusortus/f2008c485cf2e7243a8e6b8645d41d21 to your computer and use it in GitHub Desktop.

Select an option

Save idusortus/f2008c485cf2e7243a8e6b8645d41d21 to your computer and use it in GitHub Desktop.
Block-bodied Lambda Expressions: Understanding the Requirement for Explicit Return Statements

Block-bodied Lambda Expressions: Understanding the Requirement for Explicit Return Statements

var selections = model.SelectedActivityIds.Select(activityId =>
{
    // 1. For EACH activityId in the collection...
    
    // 2. Find matching sub-selections
    var subSelectionIds = model.SelectedSubOptions
        .Where(kvp => kvp.Key.StartsWith(activityId + "_"))
        .Select(kvp => kvp.Value)
        .ToList();

    // 3. Return an object for THIS iteration
    return new StrategicVisitRequestSelectionInput
    {
        StrategicVisitSelectionID = activityId,
        SubSelectionIDs = subSelectionIds
    };
}).ToList();

How It Works

The lambda expression passed as an argument to the Select() extension method uses a block body (indicated by curly braces {}), which means it contains multiple statements and therefore requires an explicit return statement to specify what value the lambda expression produces. This is different from an expression-bodied lambda, which consists of a single expression that implicitly returns its evaluated result without needing the return keyword.

Two Forms of Lambda Expression Bodies:

Expression-bodied lambda (contains a single expression that implicitly returns its evaluated result; no return keyword needed or allowed):

.Select(activityId => new SomeObject { Id = activityId })

Block-bodied lambda (contains multiple statements enclosed in curly braces; requires an explicit return statement to specify the value produced by the lambda expression):

.Select(activityId => 
{
    var temp = DoSomeWork(activityId);
    return new SomeObject { Id = activityId, Data = temp };
})

What This Code Does

The Select() extension method is invoked on the model.SelectedActivityIds collection. For each individual activity ID in that collection (for example, "ACTIVITY_5"), the lambda expression executes the following steps:

  1. Filter the dictionary: Query model.SelectedSubOptions (which appears to be a dictionary or key-value collection) using the Where() extension method to identify only those entries whose keys begin with the current activity ID followed by an underscore (e.g., keys like "ACTIVITY_5_CHASSIS", "ACTIVITY_5_TRUCKTYPE")

  2. Extract the values: Use the Select() extension method to project only the values (sub-selection IDs) from the filtered key-value pairs, discarding the keys

  3. Materialize the sub-selections: Call ToList() to immediately execute the query and store the filtered sub-selection IDs in a concrete list assigned to the subSelectionIds variable

  4. Construct the result object: Create a new instance of StrategicVisitRequestSelectionInput with two properties: the current activityId and the subSelectionIds list that was just created

  5. Return the constructed object: Use an explicit return statement (required because this is a block-bodied lambda) to provide this object as the output of the lambda expression for the current iteration

After the Select() extension method has processed all activity IDs and the lambda expression has produced one StrategicVisitRequestSelectionInput object for each activity ID, the final ToList() method call materializes the entire sequence of returned objects into a concrete List<StrategicVisitRequestSelectionInput> which is assigned to the selections variable.

Equivalent Implementation Using Expression-bodied Lambda

The same logic can be expressed using an expression-bodied lambda by embedding the filtering and extraction operations directly within the object initializer, eliminating the need for the intermediate subSelectionIds variable and the explicit return statement:

var selections = model.SelectedActivityIds.Select(activityId =>
    new StrategicVisitRequestSelectionInput
    {
        StrategicVisitSelectionID = activityId,
        SubSelectionIDs = model.SelectedSubOptions
            .Where(kvp => kvp.Key.StartsWith(activityId + "_"))
            .Select(kvp => kvp.Value)
            .ToList()
    }
).ToList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment