[英]Fastest way to get matching items from two list c#
我有兩個清單
List1只有兩個屬性。 不能使用字典,因為可能會有重復的鍵。 Property1和Property2的組合是唯一的。
public class List1
{
public string Property1 { get; internal set; }
public string Property2 { get; internal set; }
}
public class List2
{
public string Property1 { get; internal set; }
public string Property2 { get; internal set; }
public string Property3 { get; internal set; }
}
List<List1> mylist1 = new List<List1>() {
new List1() {Property1="664",Property2="Ford" },
new List1() {Property1="665",Property2="Ford" },
new List1() {Property1="664",Property2="Toyota" },
};
List<List2> mylist2 = new List<List2>() {
new List2() {Property1="664",Property2="Ford" ,Property3="USA"},
new List2() {Property1="665",Property2="Ford" ,Property3="USA"},
new List2() {Property1="664",Property2="Toyota" ,Property3="USA"},
new List2() {Property1="666",Property2="Toyota" ,Property3="USA"},
};
我需要在mylist1和mylist2中獲得匹配的項目。 匹配僅在Property1和Property2上發生。 比較期間可以忽略mylist2中的Property3。
目前我使用
var matchingCodes = mylist1.Where(l1 => mylist2.Any(l2 => (l2.Property1 == l1.Property1 && l2.Property2==l1.Property2))).ToList();
效果很好。 但是,有沒有更好的方法/最快的方法呢?
我可以將List1更改為任何其他類型。 但不是List2。
在Linq中做到這一點的最簡單方法是,它比您的方法相對更快,或者至少要快得多,是使用Join
或GroupJoin
如下所示:
List<List1> matchingCodes = mylist1.GroupJoin(mylist2,
l1 => new { l1.Property1, l1.Property2 },// Define how the key from List1 looks like
l2 => new { l2.Property1, l2.Property2 },// Define how the key from List2 looks like
// Define what we select from the match between list1 and list2
(l1Item, l2Items) => l1Item).ToList();
簡化后,這將創建兩個字典,然后將它們連接在一起。
GroupJoin
在這里效果更好,因為它為您提供了List1中的項目以及list2中的所有匹配項。
常規Join
會在列表2的每次匹配中從列表1返回相同的項目。
另請參見Enumerable.GroupJoin(C#參考)
注意,這等效於@octavioccl的答案 。 此示例還假定,兩個類的屬性名稱相同。 如果他們不滿意,則必須對其進行修改,如下所示:
l1 => new { A=l1.Foo, B=l1.Bar},
l2 => new { A=l2.Herp, B=l2.Derp},
您也可以加入:
var query= from l in mylist1
join e in mylist2 on new {Property1=l.Property1,Property2=l.Property2} equals new {Property1=e.Property1,Property2=e.Property2}
select l;
這兩個屬性都是字符串,因此您可以僅創建一個字典,並用鍵將這些屬性與實際項目的值串聯在一起 。
因此,對於其他列表中的每個項目,您都可以在字典中查找其屬性的串聯,如果存在匹配項,則將其與找到的項目進行比較
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.