14,934,026 members
See more:
```List<int> dd = new List<int>() { 1, 2, 3, 4, -1, -2, -4, 5, 9, 7, 5, 8, 9 };
```

`how to Divide list in lambda with The condition is positive or negative number`

`result =[1,2,3,4],[-1,-2,-4],[5,9,7],[7, 5, 8, 9]`

What I have tried:

`<pre>   List<int> dd = new List<int>() { 1, 2, 3, 4, -1, -2, -4, 5, 9, 7, 5, 8, 9 };`

`how to Divide list in lambda with The condition is positive or negative number`

`result =[1,2,3,4],[-1,-2,-4],[5,9,7],[7, 5, 8, 9]`
Posted
Updated 9-Jan-21 4:22am
Gerry Schmitz 9-Jan-21 10:00am

How did you manage to turn one 7 into two 7's? [...7] and [7...]

And you want the result to be a "List of lists"?
BillWoodruff 9-Jan-21 12:17pm

You have not shown any code you TRIED: you just show the result you want.
Sandeep Mewara 9-Jan-21 15:33pm

Though you have tagged C#, can you confirm its in c# or python that you are trying it?

## Solution 1

You can't really use Linq for that because Linq is not defined as a sequential execution stream - it could be out of order and you'd get well confused. It normally is executed from the start of the sequence to the end, but that isn't set in stone - a multithreaded optimisation could put it out of order and your sequences would be broken.

Instead, use simple, obvious code:
C#
```List<int> dd = new List<int>() { 1, 2, 3, 4, -1, -2, -4, 5, 9, 7, 5, 8, 9 };
bool positive = dd[0] >= 0;
List<List<int>> results = new List<List<int>>();
List<int> current = new List<int>();
foreach (int i in dd)
{
if (positive && i >= 0) current.Add(i);
else if (!positive && i < 0) current.Add(i);
else
{
positive = !positive;
current = new List<int>();
}
}
```
If you want to break a sequence when a sequence stops increasing / decreasing (and your sample data implies that but doesn't confirm it) then it's fairly obvious how to add that in.
BillWoodruff 9-Jan-21 12:22pm

I have a suspicion there's some weirdness you could do using 'groupby ... but, i am not going there :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 285 Maciej Los 190 OriginalGriff 160 Richard MacCutchan 125 k5054 60
 OriginalGriff 4,665 Richard MacCutchan 2,350 Richard Deeming 1,790 Patrice T 1,295 CPallini 1,050

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900