You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何筛选包含指定List所有元素的Obj1列表?

问题分析与解决方案

你当前的筛选逻辑错误,核心是误用了Any方法:

  • Any(item2 => lst2.Contains(item2))的作用是判断NumberList中存在任意一个元素在lst2里,这和你需要的「NumberList包含lst2所有元素」逻辑完全相反。

修正后的代码

正确逻辑是验证lst2的每一个元素都能在当前对象的NumberList中找到,需要使用All方法:

List<int> lst2 = new List<int>(){4,5};
var FinalList = lst1.Where(item => lst2.All(x => item.NumberList.Contains(x))).ToList();

结果验证

执行上述代码后,FinalList会得到你预期的结果:

{
ID = 2,
Name = "t2",
NumberList = new List<int>{1,4,5}
},
{
ID = 3,
Name = "t3",
NumberList = new List<int>{4,5,6}
}

内容的提问来源于stack exchange,提问作者Ashin Best

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 20:31:09