有没有一种方法可以删除列表中的某些重复项,但不能删除其他项?

时间:2019-12-04 04:36:21

标签: c# list linq dictionary tuples

因此,我一直在处理一个非常复杂的列表,该列表是由一部分元组的字典组成的。这是我的代码。

 var Truth = new Dictionary<Tuple<string, string, string>, bool>

         {
             {new Tuple <string,string,string>("!A","!B","!C"),false },
             {new Tuple <string,string,string>("!A","!B","C"),false },
             {new Tuple <string,string,string>("!A","B","!C"),false },
             {new Tuple <string,string,string>("!A","B","C"),false },
             {new Tuple <string,string,string>("A","!B","!C"),true },
             {new Tuple <string,string,string>("A","!B","C"),true },
             {new Tuple <string,string,string>("A","B","!C"),true },
             {new Tuple <string,string,string>("A","B","C"),true},
         };

         var trueKeyList = Truth.Where(kv => kv.Value).ToList();
        List<string> myList = new List<string>();



        Console.WriteLine("SOP is: ");
        foreach (var keyValue in trueKeyList)
        {

                Console.Write(keyValue.Key.ToString().Trim(new Char[] { ')', '(' }));
                Console.Write(" AND ");
            myList.Add(keyValue.Key.Item1);
            myList.Add(keyValue.Key.Item2);
            myList.Add(keyValue.Key.Item3);


        }
        Console.WriteLine("");


        List<string> lstStrIgnore = new List<string>() { "A!", "B!", "C!" };
        myList = myList.Distinct().Where(x => x != lstStrIgnore).ToList();

在底部附近,您会看到我尝试删除A B和C的重复项,而不是A的重复项! B!和C!。但是,这是行不通的,因为它说“ operand!=无法应用于字符串类型和列表类型”?

1 个答案:

答案 0 :(得分:2)

myList.Distinct().Where(x => !lstStrIgnore.Contains(x));

或者如果您想将自己的比较器/强制转换为大写/小写等,则

myList.Distinct().Where(x => !lstStrIgnore.Any(l => l.Equals(x)));