Reputation: 113
I have the following code snippet
string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
//convert the array into a list for easier comparison
List<string> StringtoList = lines.OfType<string>().ToList();
//get the database list here
List<string> sensitiveApps = testConnection.SelectSensitive();
//compare the 2 lists to get the difference
List<string> except = sensitiveApps.Except(StringtoList,StringComparer.OrdinalIgnoreCase);
However, I keep getting the above error, could anyone point me in the right direction?
Upvotes: 0
Views: 2816
Reputation: 3815
While Daniel's suggestion is certainly correct, I'd like to propose an alternative: use HashSet<string>
which is better designed for set-based operations like Except.
var set = new HashSet<string>(sensitiveApps, StringComparer.OrdinalIgnoreCase);
set.ExceptWith(lines);
There is no need to do the lines.OfType<string>().ToList()
as lines is IEnumerable<string>
. Then, if you do need the resulting set as a list, simply call set.ToList()
.
Hope this helps!
Edit: This assumes that the order of sensitiveApps does not matter.
Upvotes: 1
Reputation: 7737
I'm guessing the last line is throwing an exception. Try changing:
List<string> except = sensitiveApps.Except(StringtoList,StringComparer.OrdinalIgnoreCase);
to:
List<string> except = sensitiveApps.Except(StringtoList,StringComparer.OrdinalIgnoreCase).ToList();
This exception is occurring as Except
will be returning an IEnumerable<string>
.
Upvotes: 4