There is a class DB

class DB { private List<string> newList; public string Naimenovanie { get; set; } public string Url { get; set; } } 

Later I use List<DB> listDB = new List<DB>(); to insert instances of the class DB in the loop.

There is also a separate list. List<string> urls = new List<string>();

 1) Как удалить элементы listDB, которых нет в urls? 2) Как добавить элементы в listDB, которые есть в urls? 3) Если элементы есть и в listDB и в urls - то его не трогаем 

    2 answers 2

    Hello! As a solution to the first task, I propose:

     var listToDelete = listDB.Where(list=>!urls.Contains(list.Url)).ToList(); listDB.RemoveRange(listToDelete); 

    The first step is to select items that are not in the list of urls. Then remove the resulting list from the main.

    The second task (if I understood you correctly):

     foreach(var url in urls){ listDB.Add(new DB(){ Url = url }); } 

    In theory, after these operations, the listDB list will contain only those elements that are in the urls

       var result = urls.Select(x=>listDB.FirstOrDefault(y=>y.Url == x)??new DB{Url = x}).ToList(); 

      If the sheet is large, then first create a dictionary.

       var cache = listDB.ToDictionary(x=>x.Url, x=>x); var result = urls.Select(x=>cache.ContainsKey(x)?cache[x]:new DB{Url = x}).ToList();