如何在三元运算符的else部分做"无"

Cem*_*mre 0 c# ternary-operator

我想根据它们是否包含在其他列表中来过滤列表的值.如果一个元素在列表中,我将选择它,否则我想跳过它或基本上什么都不做.

以下是我试图做的事情.我怎样才能做到这一点?

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 */);
Run Code Online (Sandbox Code Playgroud)

Sac*_*hin 7

您可以使用Enumerable.Intersect方法从两个列表中获取公共值.

IEnumerable<string> commonValues = projects.Intersect(sheetNames);
Run Code Online (Sandbox Code Playgroud)