2011-02-16 83 views
1

我真的以爲我是越來越與LINQ到現在爲止,我已經有了一個一對多的關係,我需要返回SUM爲Jobitems(jobitems.Cost)的LINQ集團及SUM

List<Models.Inventory.JobItem> jobItems = BLL.Inventory.JobItem.GetAllActive(forceReload); 
List<.Models.Inventory.Job> jobs = BLL.Inventory.Job.GetAllActive(forceReload); 
    var getInvoicedItems2 = from j in jobs 
    join ji in jobItems on j.JobId equals ji.JobId 
    select new { JobCost = ji.Cost, JobId = ji.JobId}; 

Ive得到這很遠,但我不知道如何按ji組合所有.JobId

回答

3

您可以使用組加入將所有作業項目分組到一個集合中,然後獲取該集合中所有成本的總和。

var jobItems = BLL.Inventory.JobItem.GetAllActive(forceReload); 
var jobs = BLL.Inventory.Job.GetAllActive(forceReload); 
var query = from j in jobs 
      join ji in JobItems on j.JobId equals ji.JobId into items 
      select new 
      { 
       JobId = j.JobId, 
       JobCost = items.Sum(ji => ji.Cost), 
      }; 
+0

感謝傑夫,我這樣做,它似乎工作 – David 2011-02-16 09:09:51

0
var getInvoicedItems2 = from ji in jobItems 
    group ji by ji.JobId 
    into g 
    select new {JobId = g.Key, Cost = g.Sum(p => p.Cost)}; 

var getInvoicedItems = from j in jobs 
    from s in schedules 
    from i in invoices 
    from jj in getInvoicedItems2 
    where i.JobId == j.JobId && j.ScheduleId == s.ScheduleId && jj.JobId == j.JobId 
    select new { JobCost = jj.Cost, InvoiceDate = i.CreatedDate, Status = i.Status, }; 

似乎工作