Not an elegant way (, but it works)
Assuming that you have a "lines" layer with a numeric field named "id".
Assuming also that you have a "polygons" layer with a numeric field named "max_id", which represents the maximum id value of the linestrings that are within each polygon.
You can Select by Expression the linestring features that have an id value that matches with any max_id value in the polygons layer, using the lines layer as input and the following expression:
concat(
',,',
aggregate(
layer:='polygons',
aggregate:='concatenate',
expression:= to_string("max_id"),
concatenator:= ','),
',,')
LIKE
concat(
'%,',
to_string("id"),
',%')
First, the expression builds an aggregate (by concatenation) of the values in the max_id field of the polygons layer. Returns something like: '4,25,76'
.
Then, builds a new string with that one, which starts and ends with two commas. Returns something like: ',,4,25,76,,'
.
In the other hand, also builds a string with the "id" attribute for each linestring feature, which starts with %,
and ends with ,%
. Returns something like '%,4,%'
for the feature with the id value = 4.
Finally, it compares, if ',,4,25,76,,'
is like '%,4,%'
(% symbols are wildcards), and returns true
or false
. That determines if the feature must be selected or not.
DISCLAIMER: Tested with a few features. The performance for a lot of them is not guaranteed.