我想根据列表的值是否包含在其他列表中来过滤列表的值。如果一个元素在列表中,我将选择它,否则我想跳过它或基本上什么都不做。
以下是我尝试做的事情。我怎样才能做到这一点?
List<string> sheetNames = new List<string>() {"1","10"};
List<string> projects= new List<string>() {"1","2","3","4","5","6","7"};
IEnumerable<string> result =
sheetNames.Select(x => projects.Contains(x)
? x
: /*Want to do nothing here */);
答案 0 :(得分:7)
您可以使用Enumerable.Intersect方法从两个列表中获取常用值。
IEnumerable<string> commonValues = projects.Intersect(sheetNames);
答案 1 :(得分:1)
List<string> sheetNames = new List<string>() {"1","10"};
List<string> projects= new List<string>() {"1","2","3","4","5","6","7"};
IEnumerable<string> result = sheetNames.Where(x => projects.Contains(x));