2013-03-01 59 views
0

我有實體下無法在自定義實體執行聯盟

public class A 
    { 
     public string TestMethodName { get; set; } 
     public string FailedFor { get; set; } 
    } 

    public class B 
    { 
     public string TestMethodName { get; set; } 
     public string FailedFor { get; set; } 
    } 

我使用的是Windows窗體。如果我嘗試下

private void button2_Click(object sender, EventArgs e) 
     { 
      List<A> aCollection = new List<A>(); 
      List<B> bCollection = new List<B>(); 

      aCollection.Add(new A { TestMethodName = "Method1", FailedFor = "DPLServerURL" }); 
      aCollection.Add(new A { TestMethodName = "Method2", FailedFor = "DPLServerURL" }); 
      aCollection.Add(new A { TestMethodName = "Method3", FailedFor = "DPLServerURL" }); 
      aCollection.Add(new A { TestMethodName = "Method4", FailedFor = "DPLServerURL" }); 


      bCollection.Add(new B { TestMethodName = "Method1", FailedFor = "OrderXmlLocation" }); 
      bCollection.Add(new B { TestMethodName = "Method2", FailedFor = "OrderXmlLocation" }); 
      bCollection.Add(new B { TestMethodName = "Method5", FailedFor = "OrderXmlLocation" }); 

      var xx = bCollection.Union(aCollection).ToList(); 

     } 

進行聯盟,因爲我得到錯誤的udner

Error 1 Instance argument: cannot convert from 'System.Collections.Generic.List<WindowsFormsApplication1.B>' to 'System.Linq.IQueryable<WindowsFormsApplication1.A>' 

我怎樣才能做到這些實體的聯合/合併?

感謝

+0

'xx'的預期類型是什麼? – 2013-03-01 11:19:47

+0

xx應該是aCollection和bCollection的聯合。 – 2013-03-01 11:21:27

+1

aCollection和bCollection是不同的類型 – Sergio 2013-03-01 11:22:37

回答

2

更換

bCollection.Union(aCollection) 

bCollection.Union<object>(aCollection) 

這與從T每個項目創建一個IEnumerable<object>我來源。你不能做任何更聰明的事情,因爲儘管擁有相同的屬性,但你的類AB之間沒有任何聯繫。

1

你可以試試這個:

private void button2_Click(object sender, EventArgs e) 
{ 
    List<I> aCollection = new List<I>(); 
    List<I> bCollection = new List<I>(); 

    aCollection.Add(new A { TestMethodName = "Method1", FailedFor = "DPLServerURL" }); 
    aCollection.Add(new A { TestMethodName = "Method2", FailedFor = "DPLServerURL" }); 
    aCollection.Add(new A { TestMethodName = "Method3", FailedFor = "DPLServerURL" }); 
    aCollection.Add(new A { TestMethodName = "Method4", FailedFor = "DPLServerURL" }); 


    bCollection.Add(new B { TestMethodName = "Method1", FailedFor = "OrderXmlLocation" }); 
    bCollection.Add(new B { TestMethodName = "Method2", FailedFor = "OrderXmlLocation" }); 
    bCollection.Add(new B { TestMethodName = "Method5", FailedFor = "OrderXmlLocation" }); 

    var xx = bCollection.Union(aCollection).ToList(); 
} 

public class A : I 
{ 
    public string TestMethodName { get; set; } 
    public string FailedFor { get; set; } 
} 

public class B : I 
{ 
    public string TestMethodName { get; set; } 
    public string FailedFor { get; set; } 
} 

public interface I 
{ 
    string TestMethodName { get; set; } 
    string FailedFor { get; set; } 
} 
1

在其他的答案提到,因爲AB是不同的類型,你不能直接創建與這兩種類型的集合,儘管具有相同的性質的類。您有幾種選擇:

  1. 創建匿名類型具有相同的屬性AB集合
  2. 創建從基本類型/_interface_)A和B繼承/實現
  3. 創建當前基類型的ABobject)之間的集合

選項1和2爲您提供了在不投射的情況下訪問集合類型屬性的好處,而選項3則要求您將這些項目投射到另一個類別以使其有用。

一個問題是 - 爲什麼你有兩種類型似乎做完全一樣的東西