0

我有以下問題 - 我想根據簡單的查詢和除法計算我的數據庫中的字段。 首先有點關於我的數據庫結構的信息:無法弄清楚如何計算Lightswitch中的計算字段

|WorkingUnits|-1---------n-|Materials| e.i WU與材料之間的一對多關係。

這是兩張表及它們之間的關係。在WorkingUnits表中,我有一個名爲WUAmount的計算字段。另外每個工作單位都有一個'價格'。材料也一樣 - 每個人都有一個'價格'。 我最終想要完成的是總結分配給特定工作單位的所有材料的所有價格,並將其除以工作單位本身的價格。

我試圖到目前爲止做的是以下幾點:

partial void WUAmount_Compute(ref decimal result) 
    { 
     //decimal amount = 0; 
     IDataServiceQueryable<WorkingUnit> query; 
     query = from WorkingUnit in this.DataWorkspace.ApplicationData.WorkingUnits 
       where WorkingUnit.Materials != null 
       select WorkingUnit; 

     foreach (WorkingUnit detail in query) 
     { 
      result = this.WUPrice/(result + detail.Materials.Sum(s => s.Price).Value); 
     } 
} 

但我得到一個內部異常:

Cannot compare elements of type 'System.Data.Objects.DataClasses.EntityCollection`1'. 
Only primitive types, enumeration types and entity types are supported. 

我是相當新的電燈開關和LINQ和不明白這還。任何幫助表示讚賞。

回答

2

如果我理解正確的話,你應該需要的是:

partial void WUAmount_Compute(ref decimal result) 
{ 
    result = (this.Materials.Sum(x => x.Price)/this.WUPrice); 
} 
+0

究竟:)我manged由自己想出解決辦法,但無法發佈和批准我自己的答案,由於「不夠分」 。 謝謝你的迴應。這比預期的更微不足道。希望它可以幫助別人:) – lapadets

+0

不客氣。是的,我希望它也能幫助別人。 :-) –