Thursday, February 9, 2012

Linq Except with Duplicate Members

List<string> popcapGames = new List<string> { "Zuma",

"Chuzzled",

"Bejeweled",

"Plants vs Zombies",

"Zuma",

"Peggle"

};

// I want all the elements except Chuzzled.

List<string> excludingChuzzled = popcapGames.Except(popcapGames.Where(x => x == "Chuzzled")).ToList();

// I expect excludingChuzzled to be: { "Zuma",

// "Bejeweled",

// "Plants vs Zombies",

// "Zuma",

// "Peggle"

// };

//

//

//

//

//

// but actually its: { "Zuma",

// "Bejeweled",

// "Plants vs Zombies",

// "Peggle"

// };

// What happened to my duplicate "Zuma"?

//

// I don't like Except any more.

var

My colleagues don't like the var keyword. I do.