需要合并两个数据集合的帮助

发布于 2024-09-28 12:14:05 字数 547 浏览 4 评论 0原文

我需要从两个列表中检索包含给定值的所有项目。

示例:

var list1 = {
    new Dummy(){ Name = "Dummy1", Number = 1 },
    new Dummy(){ Name = "Dummy2", Number = 2 },
    new Dummy(){ Name = "Dummy3", Number = 3 }
};

var list2 = {
    new Dummy(){ Name = "Dummy4", Number = 4 },
    new Dummy(){ Name = "Dummy5", Number = 2 },
    new Dummy(){ Name = "Dummy6", Number = 6 }
};

var list3 = GetAllDummiesWithNumbersContainedInBothLists();

我希望 list3 包含 Dummy2Dummy5,因为两者具有相同的编号。

我该怎么做?应该很简单,但我无法弄清楚......

I need to retrieve all items from two lists that contains a given value.

Example:

var list1 = {
    new Dummy(){ Name = "Dummy1", Number = 1 },
    new Dummy(){ Name = "Dummy2", Number = 2 },
    new Dummy(){ Name = "Dummy3", Number = 3 }
};

var list2 = {
    new Dummy(){ Name = "Dummy4", Number = 4 },
    new Dummy(){ Name = "Dummy5", Number = 2 },
    new Dummy(){ Name = "Dummy6", Number = 6 }
};

var list3 = GetAllDummiesWithNumbersContainedInBothLists();

I want list3 to contain Dummy2 and Dummy5, since both have the same number.

How do i do this? It should be simple but i cant figure it out...

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

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

发布评论

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

评论(4

软甜啾 2024-10-05 12:14:06

看看这是否适合您:

(from dummy1 in list1
join dummy2 in list2 on dummy1.Number equals dummy2.Number
from dummy in new[] { dummy1, dummy2 }
select dummy)
.Distinct()

这会将匹配的虚拟对象配对到同一范围内,然后将集合展平,以便您在一个序列中获得所有匹配项。末尾的 Distinct 确保每个虚拟值恰好出现一次,即使任一列表包含重复的数字也是如此。

See if this works for you:

(from dummy1 in list1
join dummy2 in list2 on dummy1.Number equals dummy2.Number
from dummy in new[] { dummy1, dummy2 }
select dummy)
.Distinct()

This pairs matching dummies into the same scope, then flattens out the set so you get all of the matches in one sequence. The Distinct at the end ensures that each dummy appears exactly once even if either list contains repeated numbers.

沙与沫 2024-10-05 12:14:06

我不完全确定你的要求是什么,但也许是这样的?

var commonIds = list1.Select(d => d.Number)
                     .Intersect(list2.Select(d => d.Number));

var commonIdsSet = new HashSet<int>(commonIds);

var list3 = list1.Concat(list2)
                 .Where(d => commonIdsSet.Contains(d.Number))
                 .ToList();

如果您能明确具体要求(结果是否需要按Number分组、Number对于列表中的项目是否唯一等),我们可以提供更好的解决方案。

I'm not entirely sure what your requirements are, but something like this perhaps?

var commonIds = list1.Select(d => d.Number)
                     .Intersect(list2.Select(d => d.Number));

var commonIdsSet = new HashSet<int>(commonIds);

var list3 = list1.Concat(list2)
                 .Where(d => commonIdsSet.Contains(d.Number))
                 .ToList();

if you can clarify the exact requirements (do the results need to be grouped by theNumber, IsNumberunique for an item within a list etc.), we can provide better solutions.

纵山崖 2024-10-05 12:14:06
var list3 = list1.Where(d => list2.Select(d2 => d2.Number).Contains(d.Number))
    .Union(list2.Where(d2 => list1.Select(d => d.Number).Contains(d2.Number)));
var list3 = list1.Where(d => list2.Select(d2 => d2.Number).Contains(d.Number))
    .Union(list2.Where(d2 => list1.Select(d => d.Number).Contains(d2.Number)));
江城子 2024-10-05 12:14:06

这里还有一个!

var list3 = list1
  .SelectMany(x => list2
   .SelectMany(y => 
    (y.Number == x.Number) ? new [] { x, y } : new Dummy[]{}
   )
  );

Here's one more!

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