Performance differences between different LINQ syntaxes [duplicate] - linq

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.

Related

Literal for ranges in C++11 [duplicate]

This question already has answers here:
Numerical range iterators in boost?
(3 answers)
Closed 5 years ago.
If I have this:
for (auto iSong = 1; iSong <= iMaxSongNumber; iSong++)
Can I use the new for range approach?
I understand that for containers they need a begin and end method for them to work. But if we have literal max values?
There isn't a built-in mechanism to do this: range-based for works on something for which begin and end can be called.
I wrote a blog post about how to do this: https://www.justsoftwaresolutions.co.uk/cplusplus/generating_sequences.html
Basically, you need to create a "virtual container" with iterators that update the count.

how to write better code using java8 [duplicate]

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.

Random values in F# [duplicate]

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()

A cycle in LINQ expression [duplicate]

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.

What is the ruby Regex or String #=== method/operator? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
=== vs. == in Ruby
I can't find verbose docs on this at all. The doc page is broken:
http://ruby-doc.org/core-1.9.3/String.html
The regex page uses the word "case" in two different senses (!) and I can't understand what the point is:
http://www.ruby-doc.org/core-1.9.3/Regexp.html#method-i-3D-3D-3D
And it was in use in Rails:
https://github.com/rails/rails/commit/3756a3fdfe8d339a53bf347487342f93fd9e1edb?utm_source=rubyweekly&utm_medium=email
=== is the "case equality" operator:
In Ruby, triple equals (Object#===) is, "effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements".
See http://andy-payne.com/2008/09/confusion-over-triple-equals/

Resources