LINQ Join 2 List<T> [英] LINQ Join 2 List&lt;T&gt;s

查看:31
本文介绍了LINQ Join 2 List<T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言:我不明白这是做什么的:

Preface: I don't understand what this does:

o => o.ID, i => i.ID, (o, id) => o

所以放轻松.:-)

我有 2 个需要合并的列表:

I have 2 lists that I need to join together:

// list1 contains ALL contacts for a customer.
// Each item has a unique ID.
// There are no duplicates.
ContactCollection list1 = myCustomer.GetContacts();

// list2 contains the customer contacts (in list1) relevant to a REPORT
// the items in this list may have properties that differ from those in list1.
/*****/// e.g.:
/*****/        bool SelectedForNotification; 
/*****///  may be different.
ContactCollection list2 = myReport.GetContacts();

我需要创建第三个 ContactCollection,它包含 list1 中的所有联系人,但具有 list2 中项目的属性,如果该项目在列表中[2] (list3.Count == list1.Count).

I need to create a third ContactCollection that contains all of the contacts in list1 but with the properties of the items in list2, if the item is in the list[2] (list3.Count == list1.Count).

我需要将 list1 中的所有项目替换为 list2 中的项目,其中 list1 中的项目具有项目的 ID在 list2 中.结果列表 (list3) 应在 list1 处包含相同数量的项目.

I need to replace all items in list1 with the items in list2 where items in list1 have the IDs of the items in list2. The resulting list (list3) should contain the same number of items at list1.

我感觉好像我没有任何意义.所以,请在评论中提出问题,我会尽力澄清.

I feel as though I'm not making any sense. So, please ask questions in the comments and I'll try to clarify.

推荐答案

Join 并不是那么困难,但您的问题可能需要进一步解释.

Joins are not so difficult, but your problem could probably use some further explanation.

要加入两个列表,您可以执行类似

To join two lists, you could do something like

var joined = from Item1 in list1
             join Item2 in list2
             on Item1.Id equals Item2.Id // join on some property
             select new { Item1, Item2 };

这将给出一个 IEnumerable<'a>,其中 'a 是一个匿名类型,包含来自 list1 的一个项目及其来自 list2 的相关项目.然后,您可以根据需要选择要使用的对象属性.

this will give an IEnumerable<'a>, where 'a is an anonymous type holding an item from list1 and its related item from list2. You could then choose which objects' properties to use as needed.

为了得到一个具体列表的结果,只需要调用 .ToList().你可以这样做

To get the result to a concrete list, all that is needed is a call to .ToList(). You can do that like

var list3 = joined.ToList();
// or
var list3 = (from Item1 in list1
             join Item2 in list2
             on Item1.Id equals Item2.Id // join on some property
             select new { Item1, Item2 }).ToList();

要进行左连接以选择列表 1 中的所有元素,即使列表 2 中没有匹配项,您可以执行以下操作

To do a left join to select all elements from list1 even without a match in list2, you can do something like this

var list3 = (from Item1 in list1
             join Item2 in list2
             on Item1.Id equals Item2.Id // join on some property
             into grouping
             from Item2 in grouping.DefaultIfEmpty()
             select new { Item1, Item2 }).ToList();

这将为您提供一个列表,其中 Item1 等于第一个列表中的项目,而 Item2 将等于第二个列表中的匹配项默认值,对于引用类型将为 null.

This will give you a list where Item1 equals the item from the first list and Item2 will either equal the matching item from the second list or the default, which will be null for a reference type.

这篇关于LINQ Join 2 List<T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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