c#中如何在列表中使用Except方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13055733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:30:27 来源:igfitidea点击:
How to use Except method in list in c#
提问by Lakhae
I had create a two int type list and assign the items that are only in list1 to list1 using except method. for eg
我创建了一个两个 int 类型的列表,并使用 except 方法将仅在 list1 中的项目分配给 list1。例如
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
list1 = {1,2,3,4,5,6} // get items from the database
list2 = {3,5,6,7,8} // get items from the database
list1 = list1.Except(list2); // gives me an error.
Please give me suggestion. What is the right way to do it.
请给我建议。什么是正确的方法。
采纳答案by Michal Klouda
The Exceptmethod returns IEnumerable, you need to convert the result to list:
该Except方法返回IEnumerable,您需要将结果转换为列表:
list1 = list1.Except(list2).ToList();

