I've tried the methods here:
- Compare two List<string> and print the duplicates
- Is there any way to compare two lists in C#
- https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-find-the-set-difference-between-two-lists-linq.
None of the above solved my issue.
I have two lists, List1, and List2. List2 is a .txt file read into a string and then split into a list using commas. List1 is the output of a browser handle split using '-'. So, for example, List1 has the items "Stack Overflow", "Personal", "Microsoft Edge", and List2 has the items "Personal", and "Microsoft Edge". So, there is an overlap between the two lists. I've tried the methods below, with no success.
List<string> duplicates = List2.Intersect(List1).ToList();
if (duplicates.Count > 0)
{
foreach(var dup in duplicates)
{
Console.WriteLine(dup);
}
}
else
{
Console.WriteLine("No");
}
This always will throw a no, even though I thought intersect would find ANY sort of duplicates and give it to the list. To be clear, I am hoping for the outcome "Personal" and "Microsoft Edge". I've also tried
bool match = List1.Contains(TextDoc);
if (match)
{
Console.WriteLine("Match");
}
else
{
Console.WriteLine("no Match");
}
TextDoc is just the txt document read into a string using File.ReadAllText. Neither of these options gives me a match, even though as far as I can tell both of them should given the context of the Lists. Am I missing something super obvious here?