2016-06-30 80 views
3

我有泛型類型AB的兩個列表:如何將兩個不同的列表映射到一個列表?

public class A { 
    int type; 
    string params; 
    bool isActive; 
} 

public class B { 
    int type; 
} 

我怎麼能映射他們到類型A其中B.type == A.type使用LINQ(不A.type == B.type !!)的一個列表? B類的

實例包含可以刪除或添加而A類的實例,包含從我的分貝值int值。

因此,例如:

A[0] = {1, "11", true}, A[1] = {2, "22", true}, A[2] = {3, "33", false} 

B = {2, 3} 

期望的結果包括A[1]A[2]

+5

'B.type == A.type(不A.type == B.type !!)「什麼?這些都是一樣的 – Jonesopolis

+0

也許你可以更多地解釋結果會是什麼;假設你有一個1xA列表和(單獨)一個2×B列表。結果是什麼? –

+0

來自B的數據可以更改,所以如果A不具有相同類型,則不應將其添加到新列表中。 @Jonesopolis –

回答

2

這聽起來像你的意思是「通過檢查針對第二列表中的屬性篩選,從第一個列表中的項目」 - 在這種情況下,我建議:

  1. 建立索引從第二列表:

    // create an index of the "type"s to look for 
    var index = new HashSet<int>(bList.Select(x => x.type)); 
    
  2. 使用該過濾數據

    // filter the primary list to values from the index 
    var matches = aList.FindAll(x => index.Contains(x.type)); 
    

這將非常有效地給你一個A數據的列表,該數據在bList中有相應的值。

這是可運行:

using System; 
using System.Collections.Generic; 
using System.Linq; 

class Program 
{ 
    public class A 
    { 
     public int type; 
     public string @params; 
     public bool isActive; 
    } 

    public class B 
    { 
     public int type; 
    } 
    static void Main() 
    { 
     var aList = new List<A> 
     { 
      new A { type = 1, @params = "11", isActive = true }, 
      new A { type = 2, @params = "22", isActive = true }, 
      new A { type = 3, @params = "33", isActive = false }, 
     }; 
     var bList = new List<B> 
     { 
      new B { type = 2 }, 
      new B { type = 3 }, 
     }; 
     // create an index of the "type"s to look for 
     var index = new HashSet<int>(bList.Select(x => x.type)); 

     // filter the primary list to values from the index 
     var matches = aList.FindAll(x => index.Contains(x.type)); 

     foreach (var match in matches) 
     { 
      Console.WriteLine($"{match.type}, {[email protected]}, {match.isActive}"); 
     } 
    } 
} 

與輸出:

2, 22, True 
3, 33, False 
3

你想加入這兩個列表,所以找到所有這兩個列表中的所有A

var query = from a in aList 
      join b in bList 
      on a.type equals b.type 
      select a; 
List<A> resultList = query.ToList(); 
+0

請注意,根據第二個列表中是否有重複的值,使用'join'會有影響 - 如果第二個列表中的值是唯一的,那麼這是完美的。如果第二個列表包含重複項,則「包含」檢查可能優於聯合。 –

1

這是你在找什麼手機版!

var result = arrayA.Where(a => arrayB.Select(b => b.type).Contains(a.type)).ToArray(); 
+0

請注意,這是非常低效的 - 它爲'arrayA'中的每個元素分別執行'Select',並使用窮舉線性搜索來檢查值。通過一些簡單的調整(請參閱我的答案),同一個*想法*可以變得更加高效。 –

0

如果你有兩個序列,其中兩個序列都應該匹配值,並且要採取零個或多個屬性形成第一個序列,並使用第二個序列中的零個或多個屬性Enumerable.Join.

語法看起來有點困難,但如果更經常使用ge不習慣它。

假設在你的例子你有一個對象的序列和B的對象序列:即有

IEnumerable<A> myAobjects = ... 
IEnumerable<B> myBobjects = ... 

// do the join: 
myAObjects.Join(myBobjects, // join the two sequences 
myAobject => myAobject.type, // from the A sequence take property type 
myBobject => myBobject.type, // from the B sequence also take property type 
(myAobject, myBobject) => // whenever the values of these properties equal, take: 
...); 

圓點將與您從myAobject的組合想要的提交和myBobject屬性類型的值相同。你的問題很簡單:每當一個myAobject。類型匹配myBobject.type,你需要完整的myAObject。在這種情況下的加入最後部分是:

(myAobject, myBobject) => myAobject 

如果你想別的東西回來,你會使用類似:

(myAobject, myBobject) => new 
{ 
    MyParams = myAobject.Params, 
    MyOtherValue = myBObject.type, 
} 
相關問題