首页主机资讯C#中怎么用Intersect方法过滤数据

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

时间2024-05-25 12:00:03发布访客分类主机资讯浏览951
导读:在C#中,可以使用LINQ的Intersect方法来过滤数据。Intersect方法用于返回两个集合的交集,即返回同时存在于两个集合中的元素。以下是一个示例代码,演示如何使用Intersect方法过滤数据: using System; us...

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

using System;
    
using System.Linq;
    
using System.Collections.Generic;


class Program
{

    static void Main()
    {
    
        Listint>
     list1 = new Listint>
 {
 1, 2, 3, 4, 5 }
    ;
    
        Listint>
     list2 = new Listint>
 {
 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。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: C#中怎么用Intersect方法过滤数据
本文地址: https://pptw.com/jishu/667733.html
在CentOS操作系统上怎么安装Docker C#中过滤数据时空集合会有什么影响

游客 回复需填写必要信息