2015-06-06 53 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需要什么,我不知道这个功能的存在。再次感谢。 –