2016-06-21 48 views
0

以下內容是什麼意思?在聯盟或Concat的「聯盟或Concat中的類型是否按不同順序分配了成員」是什麼意思?

類型都分配在不同的順序成員

我試圖連接兩個子類,並在列表進行排序他們,但我得到這個錯誤。我的代碼看起來像這樣。我希望對錯誤的解釋比其他任何事情都要多,但解決方案也是值得讚賞的。

public List<InterfaceItem> GetTransactions(string id) 
    { 
     DataClasses1DataContext context = new DataClasses1DataContext(); 

     var tollQuery = from t in context.Tolls 
         where t.account_id.Equals(id) 
         select new TollTest { Date = t.status_time, Amount = t.fee, Plate = t.plate_id }; 
     var paymentQuery = from p in context.Payments 
          where p.account_id.Equals(id) 
          select new PaymentTest { Date = p.status_time, Amount = p.payment1, PaymentMethod = p.credit_type }; 

     IQueryable<InterfaceItem> toll = tollQuery; 
     IQueryable<InterfaceItem> payment = paymentQuery; 

     var all = (toll.Concat(payment)).ToList(); 
     return all; 
    } 
+0

我在這裏猜測,但通常當你合併選定的字段必須按類型匹配(並且應邏輯)。我猜(再次)plate_id和credit_type是不同的類型?當您(暫時)更改PaymentMethod = 0時會發生什麼?它在這種情況下工作嗎? –

+0

plate_id和credit_type都是字符串。 TollTest和PaymentTest都是它們繼承Date(DateTime)和Amount(小數)的InterfaceItem的子類。如果我將List作爲子類返回,它的工作狀況很好,但我不確定要加入這兩個。我試過tollQuery.Cast ()。Union(paymentQuery.Cast ())。OrderBy(x => x.Date).ToList();但這只是給了我相同的錯誤 – Max

+0

是否InterfaceItem有字段Plate和PaymentMethod?如果是這樣,請嘗試選擇或轉換爲該類型而不是您的特定類型?或者,如果這兩個字段都存在於您的子對象中,請顯式選擇它,如新的TollTest {Date = t.status_time,Amount = t.fee,Plate = t.plate_id,PaymentMethod = null} – Nikki9696

回答

0

靈感來自Nikki9696我想通了。我正在使用TollTest和PaymentTest作爲InterfaceItem的子類。最後,我刪除的子類,而是做了以下內容:

 var tollQuery = from t in context.Tolls 
         where t.account_id.Equals(id) 
         select new InterfaceItem { Date = t.status_time, Amount = t.fee, PaymentMethod = null, Plate = t.plate_id }; 
     var paymentQuery = from p in context.Payments 
          where p.account_id.Equals(id) 
          select new InterfaceItem { Date = p.status_time, Amount = p.payment1, PaymentMethod = p.credit_type, Plate = null }; 

我是能夠成功地使用子類只有當我使用的基類變量,即

  var tollQuery = from t in context.Tolls 
         where t.account_id.Equals(id) 
         select new TollTest { Date = t.status_time, Amount = t.fee}; 
     var paymentQuery = from p in context.Payments 
          where p.account_id.Equals(id) 
          select new PaymentTest { Date = p.status_time, Amount = p.payment1}; 

因此,如果您遇到此錯誤你應該確保你有相同的變量名稱,而不僅僅是它們的類型。

相關問題