C# WhereIf Extension Method for Conditional Filtering with Example
The code provided utilizes the 'WhereIf' extension method to filter a collection conditionally, based on the value of 'input.SpareNumber'.
The 'WhereIf' extension method accepts two parameters: a condition and a predicate. If the condition evaluates to 'true', the predicate is applied to the collection; otherwise, the collection remains unchanged.
In this instance, the condition is 'input.SpareNumber.IsNotNullOrWhiteSpace()', which verifies whether 'input.SpareNumber' is not null, empty, or solely composed of whitespace characters.
The predicate is a lambda expression: 'x => (x.SpareNumber || x.ToolNumber || x.SubstanceNumber).Contains(input.SpareNumber)'. This lambda expression checks if 'input.SpareNumber' exists within the 'SpareNumber', 'ToolNumber', or 'SubstanceNumber' properties of the objects within the collection.
Therefore, if 'input.SpareNumber' is not null, empty, or contains only whitespace characters, the code will filter the collection, retaining only objects where the 'SpareNumber', 'ToolNumber', or 'SubstanceNumber' properties include 'input.SpareNumber'.
原文地址: https://www.cveoy.top/t/topic/qkwT 著作权归作者所有。请勿转载和采集!