C# – LINQ – Remove duplicates from a list and ignore values from another list
My idea for the blog was to post some articles every now and then. But at least, I am trying to keep up to a schedule of minimum 1 article per week, to keep it alive. Thus, I have decided to show some further steps with LINQ in C#.
The idea is that we have two lists – one with initial data, that contains duplicates.

The second list is with data, that contains values, which should be ignored from the list. Looks like a simple outer-left-join with SQL, but here, we have LINQ. Thus, we have to use the “=>” sign at least twice. I have created two examples – the first one is just with a single ignored value, and the second one is with a whole list of ignored values. Concerning the performance, I am not sure that it is quick enough, but still it works:
using System;
using System.Collections.Generic;
using System.Linq;
class Sample
{
static void Main()
{
List<string> lstStrMyList = new List<string>() { "Vityata","Vitosh","Academy","MyAcademy","Vitosh", "Pesho", "Gosho"};
List<string> lstStrIgnore = new List<string>() { "Pesho", "Gosho", "Atanas", "Academy" };
string strIgnored = "Academy";
List<string> lstStrFiltered = lstStrMyList.Distinct().Where(x => x != strIgnored).ToList();
Console.WriteLine("List 1:");
foreach (var item in lstStrFiltered)
{
Console.WriteLine(item);
}
List<string> lstStrFiltered2 = lstStrMyList.Distinct().Where(x => !lstStrIgnore.Any(e => x.Contains(e))).ToList();
Console.WriteLine("List 2:");
foreach (var item in lstStrFiltered2)
{
Console.WriteLine(item);
}
}
}