LINQ加入2表&LT; T&GT; S [英] LINQ Join 2 List<T>s
问题描述
preface:我不明白这是什么呢:
Preface: I don't understand what this does:
o => o.ID, i => i.ID, (o, id) => o
这样下去容易对我。 : - )
So go easy on me. :-)
我有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();
我需要创建一个包含 list1的
但随着项目的列表2 $ C的属性所有联系人的三分之一ContactCollection $ C>,如果该项目在列表中[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
).
我需要替换列表2
在 list1的
与项目的所有项目,其中在<$项目C $ C> list1的有项目在列表2
的ID。结果列表(项目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.
推荐答案
加盟并没有那么困难,但你的问题也许可以使用一些进一步的解释。
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&LT;'A&GT;
,其中一个是匿名类型拿着从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();
要做到左连接选择从list1的所有元素,即使没有在列表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();
这会给你的地方项目1从第一个列表,项目2等于项目的列表要么等于从第二个列表匹配项的或的默认值,这将是空的引用类型。
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加入2表&LT; T&GT; S的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!