c# - LINQ 除了不检索重复项
问题描述
我在 c# 中有两个列表,我想检索 listaA 上所有不在 listaB 中的元素,所以我决定使用 except 方法,如下面的代码:
List<string> listaA = new List<string>() { "a", "b", "b", "c", "d", "e", "e" };
List<string> listaB = new List<string>() { "e" };
var inter = listaA.Intersect(listaB);
var excep = listaA.Except(listaB).Count();
除了输出 - > 4
但是我的预期输出应该是 5,因为我们有两个“b”,所以如果在 ListaA 中我们排除了 listaA 中存在的所有元素,为什么输出是 4 而不是 5,我怎样才能让它按我的预期工作?
解决方案
你可以做Where
,然后得到Count
的输出:
var result = listaA.Where(x =>!listaB.Contains(x));
var newcount=result.Count();
推荐阅读
- qt - 如何在 qt 设计器中对按钮进行分组
- python - 如何在熊猫数据框中的索引列中添加数据
- php - PHP将循环中的字符串与similar_text进行比较,但仅显示匹配是否唯一
- android - 针对 Android 12 时,我们可以删除 Android 清单中的所有位置权限条目吗?
- google-sheets - 索引/匹配 - 尝试查找条目时收到错误
- python - 创建新的 netcdf 变量并使用列表中的数据填充它
- gpu - cuml DBSCAN 使用 numba 设备阵列运行缓慢
- php - 在codeigniter中匹配mysql表中的两列不起作用
- python - 刻面图中的注释
- python - 如何将列表从数据库转换为数据框?