查看两个列表中匹配的项目

发布于 2024-11-09 22:37:21 字数 303 浏览 0 评论 0原文

以免说我有两个列表

List1:

“Tom”, “坦率”, “莱西”

名单2:

“弗兰克”, “Tom”

需要什么查询才能显示 Tom 和 Fran 被重复?

我想要比较的列表非常大,如果我执行以下操作:

 var q = from a in List1
         from b in List2
         where a.Name == b.Name
         select a;

这需要很长时间。

lest say I have two lists

List1:

"Tom",
"Frank",
"Lacey"

List2:

"Frank",
"Tom"

what would be the query needed to show that Tom and Fran are being repeated?

The lists that I am trying to compare are very big and if I do something like:

 var q = from a in List1
         from b in List2
         where a.Name == b.Name
         select a;

this takes a long time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悍妇囚夫 2024-11-16 22:37:21

要查看哪些值在列表中重复,您可以使用

var results = list1.Intersect(list2);

如果您对匹配项目并对每个项目执行某些操作感兴趣,您可以使用 Join

var results = from item1 in list1 
              join item2 in list2 
              on item1 equals item2 
              select new 
              {
                  // include what you want here
              };

在您的情况下,因为您正在处理一个列表对于字符串,Intersect 似乎是合适的操作过程。如果您正在处理公共键上的匹配对象列表,您可能会选择加入列表并投影结果。

To see what values are duplicated across lists, you can use

var results = list1.Intersect(list2);

If you were otherwise interested in matching the items and doing something with each, you could use Join

var results = from item1 in list1 
              join item2 in list2 
              on item1 equals item2 
              select new 
              {
                  // include what you want here
              };

In your case, since you are dealing with a list of strings, Intersect seems like the appropriate course of action. If you were dealing with matching lists of objects on a common key, you might opt to join the lists and project the results.

↘紸啶 2024-11-16 22:37:21

您应该使用Intersect

var items = List1.Intersect(List2); // Tom, Frank

You should use Intersect:

var items = List1.Intersect(List2); // Tom, Frank
多像笑话 2024-11-16 22:37:21

您可以使用相交

List<string> list3 = list1.Intersect(list2).ToList();

You can use intersect:

List<string> list3 = list1.Intersect(list2).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文