2013-03-14 27 views
1

我對LINQ很陌生,並且對我的表格進行分組時非常麻煩,所以所有內容都彙總在一起。我試圖讓這個顯示所有購買超過50美元的顧客,即使他們有多個購買。在使用linq的SQL數據庫中。這是我到目前爲止,請幫助。在Linq中使用3張表進行分組

public class TopCustomerVM 
{ 
    public string Name { get; set; } 
    public decimal DollarsSold { get; set; } 

    public static List<TopCustomerVM> GetResults() 
    { 
     ACEEntities db = new ACEEntities(); 
     return (from c in db.Customers 
       join o in db.Orders on c.CustomerId equals o.CustomerId 
       join l in db.OrderLines on o.OrderId equals l.OrderId 

       into x 
       select new TopCustomerVM {       
         Name = c.FirstName + c.LastName, 
         DollarsSold = x.Sum(asdf => asdf.UnitCost * asdf.Quantity) 
         }) 
         .OrderByDescending(lkj => lkj.DollarsSold) 
         .Where(lkj => lkj.DollarsSold > 50) 
         .ToList();    
    } 
} 
+0

我不能做到這一點,但如果您編輯您的帖子並添加carraige回請幫助之後,代碼的第一部分將正確格式化。 – 2013-03-14 01:48:57

+0

謝謝,我修好了 – user2167675 2013-03-14 01:52:01

回答

0

我相信這樣的事情會給你所需的輸出:

from record in 
    (from c in Customers 
    join o in Orders on c.Id equals o.CustomerId 
    join ol in OrderLines on o.Id equals ol.OrderId 
    let customerAndLine = new { Name = c.FirstName + ' ' + c.LastName, DollarsSold = ol.UnitCost * ol.Quantity } 
    group customerAndLine by customerAndLine.Name into grp 
    select new { Name = grp.Key, DollarsSold = grp.Sum(r => r.DollarsSold) }) 
where record.DollarsSold > 50 
orderby record.DollarsSold descending 
select record