0

So, I am having an issue with an SQL query that is translated LINQ (and works - tested), but that same LINQ query does not work in Lightswitch. Of course I did not expect to work straight out, but I am struggling to properly convert it.

So here is a image of the tables that I base my query on:

http://dl.dropbox.com/u/46287356/tables.PNG

(sorry for outside link, but not enough rep points :))

The SQL query is the following:

SELECT WorkingUnits.Name AS WUName, ContractPositions.WUInstanceId,
   Materials.Cost, BillingValues.Value, BillingValues.PricePerUnit
FROM WorkingUnits 
  INNER JOIN
   Materials ON WorkingUnits.Id = Materials.Material_WorkingUnit 
  INNER JOIN ContractPositions ON 
   Materials.Id = ContractPositions.ContractPosition_Material 
  INNER JOIN BillingValues ON 
   ContractPositions.Id = BillingValues.BillingValue_ContractPosition

Now, I have transformed this to LINQ in the following way:

 var query = from wu in this.DataWorkspace.ApplicationData.WorkingUnits
             join m in this.DataWorkspace.ApplicationData.Materials on 
                new { Id = WorkingUnits.Id } equals new { Id = m.Material_WorkingUnit }
             join cp in this.DataWorkspace.ApplicationData.ContractPositions on 
                new { Id = m.Id } equals new { Id = cp.ContractPosition_Material }
             join bv in this.DataWorkspace.ApplicationData.BillingValues on 
                new { Id = cp.Id } equals new { Id = bv.BillingValue_ContractPosition }
             select new
             {
                 usage = bv.Value * bv.PricePerUnit,
                 totalCost = (bv.Value * bv.PricePerUnit) * m.Cost, 
                 amount = (bv.Value*bv.PricePerUnit) * m.Cost / wu.WUPrice
             };

Notice that I have changed a few things - like section of colums, as I do not need that in Lightswitch.

So while this works agains the SQL server, Lightswitch complains that I must consider explicitly specifying the type of the range variable 'WorkingUnits'. I tried to cast it, but then there are other errors such as:

 'int' does not contain a definition for 'Id' and no extension method 'Id' 
  accepting a first argument of type 'int' could be found (are you missing 
  a using directive or an assembly reference?)

So my questions is, how do I properly convert that query and expect it to work? Also, If we take that my database is setup correctly, do I even need to use 'joins' in the LINQ? Any ideas are appreciated!

4

2 回答 2

0

尝试这样的事情

var query = from wu in this.DataWorkspace.ApplicationData.WorkingUnits
                 join m in this.DataWorkspace.ApplicationData.Materials on 
                   wu.Id  equals m.WorkingUnitID }
                 join cp in this.DataWorkspace.ApplicationData.ContractPositions on 
                    m.Id  equals cp.ContractPosition_Material 
                 join bv in this.DataWorkspace.ApplicationData.BillingValues on 
                   cp.Id equals bv.BillingValue_ContractPosition 
                 select new
                 {
                     usage = bv.Value * bv.PricePerUnit,
                     totalCost = (bv.Value * bv.PricePerUnit) * m.Cost, 
                     amount = (bv.Value*bv.PricePerUnit) * m.Cost / wu.WUPrice
                 };
于 2013-01-31T09:17:14.800 回答
0

从底部(BillingValues)开始并使用实体引用向上工作呢?

例如

  var query = from bv in this.DataWorkspace.ApplicationData.BillingValues
     let m = bv.ContractPosition.Material  
     let wu = m.WorkingUnit
     select new
     {
          usage = bv.Value * bv.PricePerUnit,
          totalCost = (bv.Value * bv.PricePerUnit) * m.Cost, 
          amount = (bv.Value*bv.PricePerUnit) * m.Cost / wu.WUPrice
     };
于 2013-01-31T11:35:19.877 回答