在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。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/970656.html