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.
This comment has been removed by the author.
ReplyDeleteSo you really do want to keep one duplicate but remove another?
ReplyDeleteYou can probably imagine that Except adds the lists together in RAM then performs a distinct.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Coderoo1
{
class Program
{
static void Main(string[] args)
{
List popcapGames = new List {
"Zuma",
"Chuzzled",
"Bejeweled",
"Plants vs Zombies",
"Zuma",
"Peggle"
}.Except(new List{"Chuzzled"}).ToList();
popcapGames
.ForEach(s => Console.WriteLine(s));
}
}
}
// I will see how I can change this to keep both copies of Zuma
This will work, but it does not use Except:
ReplyDeleteusing System;
using System.Collections.Generic;
using System.Linq;
namespace Coderoo1
{
class Program
{
static void Main(string[] args)
{
List popcapGames = new List {
"Zuma",
"Chuzzled",
"Bejeweled",
"Plants vs Zombies",
"Zuma",
"Peggle"
};
popcapGames.Where(s => !s.Equals("Chuzzled")).ToList()
.ForEach(s => Console.WriteLine(s));
}
}
}