Alright, so I am currently trying to find all strings in a list that don’t exist in another list of strings. Here is what my code currently looks like:
var items = GetLiveItems(); // Returns List<string>
var currentItems = GetCurrentItems(); // Returns List<string>, Exception here
var rogueItems = items.Where(i => !currentItems.Contains(i)).ToList();
When I run the above code, I get a System.NullReferenceException on the second line. If I get rid of the 3rd line (items.Where…), the exception goes away. The two lists are rather large, first having 180k strings, second being 290k strings, but they are only 12 characters long each.
What could the issue be?
EDIT:
public static List<string> GetCurrentItems()
{
var db = DatabaseFactory.CreateDatabase("DB");
var command = db.GetStoredProcCommand("getItems");
var items = new List<string>();
using (var reader = SafeSqlReader(db.ExecuteReader(command)))
{
while (reader.Read()) items.Add(reader.GetString("name"));
}
return items;
}
EDIT:
So using the below code:
var rogueItems = items.Except(currentItems).ToList();
worked. Can anyone explain why my previous method didn’t? And the other suggested methods.
EDIT:
So, since I am having a hard time reproducing this in a project I can share with everyone to take a look, I want to provide this screen shot to prove I am not crazy.
EDIT:
Here is a screen shot with the Except logic and you can see I successfully stepped over the line I got an exception before.


Try using the
EXCEPTLinq method – http://code.msdn.microsoft.com/LINQ-Set-Operators-374f34fe rather thanWHERE !Contains