2015-06-06 54 views
1

可以說我有3個表選擇從多個表,圖1對象

  1. 購買
  2. 銷售
  3. LostInventory

而且simplifing讓我們定義這些類

public class Purchase { 
    public int Id {get;set;} 
    public int ProductId {get;set;} 
    public int PurchasedQ {get;set;} 
} 
public class Sales{ 
    public int Id {get;set;} 
    public int ProductId {get;set;} 
    public int SoldQ {get;set;} 
} 
public class LostInventory { 
    public int Id {get;set;} 
    public int ProductId {get;set;} 
    public int LostQ {get;set;} 
} 

時光流逝,我們有3個購買es,5個銷售額和2個LostInventory對象。

現在我想讓用戶知道他的產品發生的歷史。

爲了這個,我是在這些對象映射到一個名爲historyItem類新思維

public class HistoryItem 
{ 
    public int Q {get; set;} 
    public int Type {get;set;} //specifies if the product history comed from a purchase, a sale or a lost 
    public int ItemId {get;set;} //specifies the id of the object in its table (according to Type) 
} 

是我的主意好嗎?如果是的話我怎麼能achivbe這個?我其實不能找到這個一個好的解決方案,這是我努力

var targetProduct = 1; // an example target 

IQueryable<ProductHistory> purchasesQuery = db.purchases 
      .Where(x => x.ProductId == targetProduct) 
      .Select(x => new HistoryItem 
       { 
        Q = x.PurchasedQ, 
        Type = 1, 
        ItemId = x.Id 
       }); 

IQueryable<ProductHistory> salesQuery = db.sales 
      .Where(x => x.ProductId == targetProduct) 
      .Select(x => new HistoryItem 
       { 
        Q = -x.SoldQ, 
        Type = 2, 
        ItemId = x.Id 
       }); 
IQueryable<ProductHistory> lostQuery = ... 

//I need to return an Iqueryable containing them all 
return purchasesQuery + salesQuery + lostQuery //how to solve this?? 

我需要返回一個IQueryable,因爲它是一個OData的網頁API控制器 感謝的一部分。

回答

4

如何

purchasesQuery.Concat(salesQuery.Concat(lostQuery)) 

purchasesQuery.Union(salesQuery.Union(lostQuery)) 
+0

感謝您的幫助,我that's需要什麼,我不知道這個功能的存在。再次感謝。 –