consider the following example:
public IEnumerable<String> Test ()
{
IEnumerable<String> lexicalStrings = new List<String> { "test", "t" };
IEnumerable<String> allLexicals = new List<String> { "test", "Test", "T", "t" };
IEnumerable<String> lexicals = new List<String> ();
foreach (String s in lexicalStrings)
lexicals = lexicals.Union (allLexicals.Where (lexical => lexical == s));
return lexicals;
}
I’d hoped for it to produce “test”, “t” as output, but it does not (The output is only “t”). I’m not sure, but may have to do something with the deferred processing. Any ideas how to get this to work or for a good alternative?
Edit: Please note that this is just a simplified example. lexicalStrings and allLexicals are different types in the original code. So I cannot directly combine these.
Edit2 the problem to solve looks more like this:
public IEnumerable<Lexical> Test ()
{
IEnumerable<String> lexicalStrings = new List<String> { "test", "t" };
IEnumerable<Lexical> allLexicals = new List<Lexical> { ... };
IEnumerable<Lexical> lexicals = new List<Lexical> ();
foreach (String s in lexicalStrings)
lexicals = lexicals.Union (allLexicals.Where (lexical => lexical.Text == s));
return lexicals;
}
You are using wrong operation as other answer explaining. But still it is interesting why your code works incorrectly despite looking fine.
let’s modify your app a bit:
what output do you expect? here is it:
interesting, is not it?
now let’s modify it again:
now the output and results are fine:
Why does it happen? You use closure – the use of outer var in inner lambda. Since you do not actually iterate your sequence the current value of s doesn’t get into the lambda. foreach exits and all inner copies of
shold value of last iteration. In case of inner variable they hold values copies that are created for every iteration. This conflict comes from inner lazyness of LINQ. If you do things likeList.AddRangeinside loop result will be fine, becauseList.AddRangeforces iteration.