1st statement:
IEnumerable<char> query = "Not what you might expect"; query = query.Where (c => c != 'a'); query = query.Where (c => c != 'e'); query = query.Where (c => c != 'i'); query = query.Where (c => c != 'o'); query = query.Where (c => c != 'u');
Output of String.Join("", query)
: "Nt wht y mght xpct"
2nd statement:
query = "Not what you might expect"; foreach (char vowel in "aeiou") query = query.Where (c => c != vowel);
Output of String.Join("", query)
: "Not what yo might expect"
The outputs from these statements are different. Can any one explain why?
How to remove the duplicates in the list using linq? You can also do var set = new HashSet<int>(); var uniques = items. Where(x => set. Add(x.Id)); .
The => operator can be used in two ways in C#: As the lambda operator in a lambda expression, it separates the input variables from the lambda body. In an expression body definition, it separates a member name from the member implementation.
Yes, it's slower.
If you're using a C# version lower than 5.0 (where this was fixed), this is the reason:
The lambda in your query captures the loop variable vowel
.
Because Linq likes to use deferred execution, the value of this reference is not read until the query gets executed (by iterating over it), which is after the foreach
loop has finished. At that point, the most recent value of vowel
is u
, which is why you get the unexpected output.
You can get around this by copying the value to another temporary variable (or by upgrading to C# 5.0).
Try this:
query = "Probably what you might expect"; foreach (char vowel in "aeiou") { char currentVowel = vowel; query = query.Where (c => c != currentVowel ); }
It's because you create a closure over the vowel
variable, which changes in time. Store its value in a separate variable and it will work:
query = "Not what you might expect"; foreach (char vowel in "aeiou") { var current = vowel; query = query.Where (c => c != current); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With