簡體   English   中英

從兩個列表中獲取匹配項的最快方法C#

[英]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中做到這一點的最簡單方法是,它比您的方法相對更快,或者至少要快得多,是使用JoinGroupJoin如下所示:

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;

您正在嘗試對LINQ中的數據列表進行set操作。 您可以使用四個LINQ函數調用來使當前代碼更簡潔。 這些操作是:

  • 除了
  • 聯盟
  • 相交
  • 不同

您要尋找的是相交

返回在兩個單獨的集合中找到相同的值的集合Source

最后,如果您始終要使用這些特定的屬性來檢測相等性和/或唯一性,則需要為List1和/或List2類重寫Equals 這取決於誰在相交的左手邊(。之前的變量)和誰在相交的右手邊(傳遞到函數中的變量)。

如果您不知道如何覆蓋Equals函數, 這是一個SO答案 巧合的是,它也有一個Intersect例子。

這兩個屬性都是字符串,因此您可以僅創建一個字典,並用鍵將這些屬性實際項目的值串聯在一起

因此,對於其他列表中的每個項目,您都可以在字典中查找其屬性的串聯,如果存在匹配項,則將其與找到的項目進行比較

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM