2013-02-15 28 views
0

有2只列出了包含不同的數據,但也有類似的列。 基本上我想加入這些列表,但隨後同樣列合併成1如何合併列我一起

var listCombo= List1.Where(a=>DataIds.Contains(a.dataId)).DefaultIfEmpty() 
    .Join(List2, 
    list1 => list1.key, 
    list2 => list2.key, 
    (L1,L2) => new 
    { 
     L2.key, 
     L2.dataId, 
     L2.dataValue, 
     L2.date, 
     L1.secId, 
     L1.dataId, 
     L1.dataValue 
    }); 

我想在數據ID和dataValue列結合在一起。我怎麼做?

所以我只能說listCombo.dataId或listCombo.dataValue而不是唯一名字。

回答

0

如果我理解你的問題,你試圖將兩個字段合併爲一個。假設你有一個Class

public class List1 { 
public int dataId {get;set;} 
public string dataValue {get;set;} 
public string combinedValue {get;set;} 
} 

不,你可以像使用,

var listCombo= List1.Where(a=>DataIds.Contains(a.dataId)).DefaultIfEmpty() 
    .Join(List2, 
    list1 => list1.key, 
    list2 => list2.key, 
    (L1,L2) => new List1 
    { 
     dataId = L2.dataId, 
     dataValue = L2.dataValue 
     combinedValue = L2.dataId + " - " L2.dataValue 
    }); 
0

我會用POCO方式,一種是列表1,列表2,第三列表是兩者的組合。我敢肯定,如果你將不同類型的組合進行適當的轉換,或者如果你想要複雜的類型,你可以將它們定義爲它們自己的屬性或不是。

static void Main(string[] args) 
     { 
      List<A> lisA = new List<A> {new A {AId = 1, AName = "Brett"}, new A {AId = 2, AName = "John"}}; 
      List<B> lisB = new List<B> {new B { BId = 1, BName = "Doe" }, new B { BId = 2, BName = "Howard" } }; 

      List<C> lisC = lisA.Join(lisB, 
            list1 => list1.AId, 
            list2 => list2.BId, 
            (L1, L2) => new C 
             { 
              CId = L1.AId, 
              CName = L1.AName + " " + L2.BName 
             }).ToList(); 

      lisC.ForEach(
       n => Console.WriteLine(n.CName + "\n") 
       ); 

     } 

     public class A 
     { 
      public int AId { get; set; } 
      public string AName { get; set; } 
     } 

     public class B 
     { 
      public int BId { get; set; } 
      public string BName { get; set; } 
     } 

     public class C 
     { 
      public int CId { get; set; } 
      public string CName { get; set; } 
     }