在 C# 中的多个列表中查找公共项 [英] find common items across multiple lists in C#

查看:23
本文介绍了在 C# 中的多个列表中查找公共项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个通用列表:

List<string> TestList1 = new List<string>();
List<string> TestList2 = new List<string>();
TestList1.Add("1");
TestList1.Add("2");
TestList1.Add("3");
TestList2.Add("3");
TestList2.Add("4");
TestList2.Add("5");

在这些列表中查找常见项目的最快方法是什么?

What is the fastest way to find common items across these lists?

推荐答案

假设您使用具有 LINQ 的 .Net 版本,您可以使用 Intersect 扩展方法:

Assuming you use a version of .Net that has LINQ, you can use the Intersect extension method:

var CommonList = TestList1.Intersect(TestList2)

这篇关于在 C# 中的多个列表中查找公共项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆