1

我对 LINQ 非常陌生,并且在对表格进行分组时遇到了很大的麻烦,因此所有内容都加在一起。我试图让这个显示所有购买超过 50 美元的客户,即使他们有多次购买。在 SQL 数据库中使用 linq。这是我到目前为止所拥有的,请帮助。

 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();            
    }
}
4

1 回答 1

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
于 2013-03-14T03:28:31.953 回答