This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A cycle was detected in a LINQ expression exception
I have a small problem. I have 2 IQueryable (A and B). I want to find the complement numbers. For this i use Except: A.Except(B)
This will give me all the number in A that ARE NOT in B.
This works. My problem is that i want to do this A = A.Except(B)
But this leaves me with an ERROR:
A cycle was detected in a LINQ expression exception
Anyone got a suggestion on how I could solve this.
I can't just create a new IQueryable C to hold the A.Except(B) result. Because I need A repediatly in later code.
Just create an intermediate list which you use to store your result.
var C = A.Except(B).ToArray();
C will hold your desired result, while A and B will remain unchanged.
Related
This question already has answers here:
Why is a ConcurrentModificationException thrown and how to debug it
(8 answers)
Closed 3 years ago.
I am programming a sorting system to school that must split some people into two cars, but I am stuck and I don't know how to continue.
When I delete the second when, it was working. But I need it there so I don't know what to do.
I already tried iterators but I am new in kotlin and it didn't work.
for (firstPeople in firstCar){
when {
k.contains(firstPeople.toString()) -> secondCar.add(j)
// println(secondCar)
k.contains(firstPeople.toString()) -> firstCar.add(j)
// println(firstCar)
else -> when {
firstCar.size > secondCar.size -> secondCar.add(j)
firstCar.size < secondCar.size -> firstCar.add(j)
else -> firstCar.add(j)
}
}
}
Error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at MainKt.main(Main.kt:65)
at MainKt.main(Main.kt)
Thank you so much for the answer.
Looks like you are using and ArrayList. And you are inserting an item in it while iterating via iterator: (firstCar.add(j). Here is what its JavaDoc says:
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
It means that you may modify the collection only using the iterator methods, and not directly.
Try to avoid list modification, or use iterator's add method, or use copy on write collections.
This question already has answers here:
Lambda expression to convert array/List of String to array/List of Integers
(10 answers)
Closed 6 years ago.
I am converting my project to java8. How would I write this code in a better way using java8?
List<Bar> bars = new ArrayList<>();
for (Foo foo : obj.getFooList()) {
bars.add(Helper.fooToBar(foo));
}
return detailsVos;
Stream the list, mapping using a method reference, then collect to a list and return it:
return obj.getFooList().stream().map(Helper::fooToBar).collect(Collectors.toList());
Note that "better" has been interpreted as "neater" and "using the Java 8 style".
Also note that this may perform slightly worse than your original code, due to the overhead of using a stream.
This question already has answers here:
F# Functions vs. Values
(4 answers)
Closed 7 years ago.
I have F# code something like this:
let ran = new System.Random()
let makeVal = ran.NextDouble()
Why when I use makeVal do I get the same random number on every call within one run of the app session (i.e. it's not a seed issue).
Values in F# are immutable by default. So makeVal will not change after the first binding. To get different random values you should call ran.NextDouble() again.
For example use the function:
let makeVal() = ran.NextDouble()
This question already has answers here:
Should a function have only one return statement?
(50 answers)
Is good to call function in other function parameter?
(3 answers)
Closed 9 years ago.
I've got a style question. It's something I've been doing since forever, but I can't figure out why, exactly.
In most languages I've used, you can call a method that returns a value as an argument to another method:
foo(bar())
which is equal to
var bar=bar()
foo(bar)
For some reason, the latter seems unsavory. Why is that? Is the first more readable, efficient, or clean?
It's not necessarily equal.
foo(bar());
means "call bar and pipe its arguments to foo"
var retBar = bar();
foo(retBar);
means "initalize retBar, then call bar, store whatever it returns to retBar, and then call foo with retBar as its argumnet."
Depending on how expensive variables are to declare, the latter may have a larger memory footprint or slower runtime.
Really, though, it's an entire extra statement -- two extra statements, actually, depending on language -- and it leaves your code less clean. The only time I do method #2 is when I have some reason to use bar()'s value, even if only to peek at it in a debugger.
I feel its a blend of all what you said. The former construct is favorable since
a. It prevent the declaration of an additional variable to achieve the same result.
b. Its more cleaner since its more easier to read/ understand
var accountBalance = sum ( principalAmount + calculateInterest() )
than
var varCalculateInterest = calculateInterest();
var accountBalance = sum ( principalAmount + calculateInterest() )
c. If you use features like recursion, you obviously will try the former. You will need a lot of temp variables to store the intermediate results. Please see an example below.
return concatenate(quicksort(less), pivot', quicksort(greater))
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
.NET LINQ query syntax vs method chain
Are there any performance differences between writing a LINQ statement like this:
var result = (
from u in Users
where u.Searchable.Contains(searchString)
select u);
vs. like this:
var result =
Users.Where(u=>u.Searchable.Contains(searchString));
There is no performance difference as the first query is just syntactic sugar for the second one.