2011-12-29 68 views
1

我想從編譯的查詢中返回一個匿名類型,它從兩個表中選擇多個列。返回來自已編譯的查詢委託的匿名類型

我試着使用:

public static Func < DBEntities, string> 

,但無法進行編譯。我試圖創建一個新的數據類型BOMWorkOrder,但無法使它工作。可能會缺少一些語法。

public static Func<DBEntities, string, IQueryable<BOMWorkOrder>> compiledWorkorderQuery = 
     CompiledQuery.Compile((DBEntities ctx, string bomNumber) => 
      from items in ctx.BM10200 
      from orders in ctx.BM10300 
      where orders.Parent_Component_ID == -1 && 
        orders.ITEMNMBR == bomNumber && 
        orders.TRX_ID == items.TRX_ID 
      select new 
      { bomWorkOrder = 
       items.TRXDATE, 
       orders.TRX_ID, 
       orders.ITEMNMBR, 
       orders.Assemble_Quantity 
      }); 

那裏的工作順序將是:

public class BOMWorkOrder 
{ 
    public DateTime TransactionDate { get; set; } 
    public string TransactionId { get; set; } 
    public string ItemNumber { get; set; } 
    public int AssemblyQuantity { get; set; } 
} 

回答

3

既然你已經創建類型BOMWorkOrder,使用這種類型,而不是一個匿名類型:

... select new BOMWorkOrder 
    { 
     TransactionDate = items.TRXDATE, 
     TransactionId = orders.TRX_ID, 
     ItemNumber = orders.ITEMNMBR, 
     AssemblyQuantity = orders.Assemble_Quantity 
    }; 
+0

它的工作。謝啦 :) – 14578446 2011-12-29 22:30:18

0

如果返回的匿名對象的列表,你將不能訪問屬性(除非您使用dynamic

你」只是在你的select中缺少類型名稱:

... 
select new BOMWorkOrder 
{ 
    TransactionData = items.TRXDATE, 
    TransactionId = orders.TRX_ID, 
    ItemNumber = orders.ITEMNBBR, 
    AssemblyQuantity = orders.Assemble_Queantity, 
} 
0
select new bomWorkOrder 
    { 
     TransactionDate =items.TRXDATE, 
     TransactionId =orders.TRX_ID, 
     ItemNumber =orders.ITEMNMBR, 
     AssemblyQuantity =orders.Assemble_Quantity 
    });