如何“无所事事”在三元运算符的else部分 [英] How to do "nothing" in the else part of ternary operator

查看:93
本文介绍了如何“无所事事”在三元运算符的else部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据列表中的值是否包含在其他列表中来对其进行过滤。如果元素在列表中,我将选择它,否则我想跳过它或基本上什么也不做。

I want to filter values of a list based on whether or not they are contained in some other list. If an element is in the list I will select it, else I want to skip it or basically do nothing.

下面是我试图做的。我该如何实现?

Below is what I tried to do. How can I achieve this?

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 */);


推荐答案

您可以使用 Enumerable.Intersect 方法以获取常用值

You can use Enumerable.Intersect method to get the common values from the two lists.

IEnumerable<string> commonValues = projects.Intersect(sheetNames);

这篇关于如何“无所事事”在三元运算符的else部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆