Problematic behavior of Linq Union?!

Posted by Foxfire on Stack Overflow See other posts from Stack Overflow or by Foxfire
Published on 2010-06-01T12:37:31Z Indexed on 2010/06/01 12:43 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

Hi,

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ