Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq aggregate on accumulated bool values

Tags:

c#

linq

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:

  • First iteration: "first", "second" (returns true)
  • Second iteration: "second", "third" (returns true)
  • Third iteration: "third", "fourth" (returns false)

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

like image 912
ohadinho Avatar asked Oct 22 '25 05:10

ohadinho


1 Answers

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.

like image 161
Groo Avatar answered Oct 24 '25 18:10

Groo