# Linq implementation on Java

**URL:** https://talk.remobjects.com/t/linq-implementation-on-java/21810
**Category:** C#
**Tags:** java, cooper
**Created:** [April 16, 2020, 9:42pm UTC](https://talk.remobjects.com/t/linq-implementation-on-java/21810 "2020-04-16T21:42:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![simon](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@simon](https://talk.remobjects.com/u/simon)
#### Post date: [April 16, 2020, 9:42pm UTC](https://talk.remobjects.com/t/linq-implementation-on-java/21810/1 "2020-04-16T21:42:52Z")

</div>

Hi!

I just fond a problem on how Linq Sequences are handled on Java implementation. The problem can be found when you run a Linq query over another Linq result.

```
        var aSequence = new List<int>{ 0, 1, 2}.Select(i => i);
        for (var i = 0 ; i < 2; i++)
        {
            for (var j = 0 ; j < 2; j++) {
                writeLn("{0}, {1} => {2}", i, j, aSequence.Any(v =>
                {
                    writeLn("predicate: {0}, {1}, {2}", i, j, v);
                    return v == i || v == j;
                }));
            }
        }

```

It will output:

```
predicate: 0, 0, 0
0, 0 => true
predicate: 0, 1, 1
0, 1 => true
predicate: 1, 0, 2
1, 0 => false
1, 1 => false

```

But it should be:

```
predicate: 0, 0, 0
0, 0 => true
predicate: 0, 1, 0
0, 1 => true
predicate: 1, 0, 0
1, 0 => true
predicate: 1, 1, 0
predicate: 1, 1, 1
1, 1 => true

```

Looking at the generated code inside cooper.jar I found the problem is you are implementing Iterator and Iterable on the same class:

```
         private final class $Where$d__0<T> implements Iterator, Iterable {

```

`Iterable` should return a new `Iterator` on each call to `Iterator<T> iterator();` but this class is always returning it self, so it is the same instance. Providing the same instance causes it to keep the iterator state, which implies you can only run over the elements of this `Iterable` once over its lifetime.

Thanks!

---

<div class="post-metadata">

### Author: ![simon](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@simon](https://talk.remobjects.com/u/simon)
#### Post date: [April 16, 2020, 9:52pm UTC](https://talk.remobjects.com/t/linq-implementation-on-java/21810/2 "2020-04-16T21:52:20Z")

</div>

There are 10 classes that implements both interfaces on the generated class: `__Extensions`. So all of them have the same issue.

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [April 16, 2020, 10:09pm UTC](https://talk.remobjects.com/t/linq-implementation-on-java/21810/3 "2020-04-16T22:09:54Z")

</div>

Thanks, logged as [bugs://84192](https://www.remobjects.com/portal/bugs/?id=84192)

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [June 17, 2020, 11:44am UTC](https://talk.remobjects.com/t/linq-implementation-on-java/21810/4 "2020-06-17T11:44:48Z")

</div>

[bugs://84192](https://www.remobjects.com/portal/bugs/?id=84192) got closed with status fixed.
