I have a list with strings:
List<string> strList = new List<string>();
strList.Add("first");
strList.Add("second");
strList.Add("third");
strList.Add("fourth");
I need to send two strings on every iteration to some method that returns bool and finally tell what was the result.
For instance:
So return value from all calls is false (performing "&&" between those return values) .
I have to use LINQ so I've try to do something like:
strList.Aggregate<bool>((prev, current) => Method(prev,current));
but unfortunately I'm doing something wrong.
Further explanation:
I want to call bool Method(string, string) for all successive string pairs in strList and get the boolean && of all the results
One way might be to create the pairs, and then call Enumerable.All to see if any of them is false:
// create tuples
var pairs = strList
.Zip(strList.Skip(1), Tuple.Create);
// check if all are true
var result = pairs.All(p => Method(p.Item1, p.Item2));
However, this is short-circuting (will stop evaluating after the first false is encountered), and after the edit, it seems you want to evaluate the whole list.
In that case, you can use Aggregate<Tuple<string>, bool> (i.e. the overload which has an accumulator of a different type) after you've got the tuples. Start with true, and use the boolean &:
var finalResult = strList
.Zip(strList.Skip(1), Tuple.Create)
.Aggregate(true, (val, tuple) => Method(tuple.Item1, tuple.Item1) & val);
If your method would accept a Tuple, it would perhaps be slightly more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With