C#中怎么用Intersect方法过滤数据


在C#中,可以使用LINQ的Intersect方法来过滤数据。Intersect方法用于返回两个集合的交集,即返回同时存在于两个集合中的元素。以下是一个示例代码,演示如何使用Intersect方法过滤数据:

using System;using System.Linq;using System.Collections.Generic;class Program{static void Main(){List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };List<int> list2 = new List<int> { 3, 4, 5, 6, 7 };var intersectList = list1.Intersect(list2).ToList();foreach (int num in intersectList){Console.WriteLine(num);}}}

在上面的示例中,我们首先创建了两个包含整数的列表list1和list2。然后使用Intersect方法获取这两个列表的交集,结果存储在intersectList中。最后,遍历intersectList并输出结果。最终的输出结果将包含两个列表中同时存在的元素3、4和5。


上一篇:PHP中怎么用array_search()查找第一个匹配项

下一篇:Kubernetes中Service和Ingress怎么使用


C#
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器