0

私は LINQ を試してみることにしましたが、これまでのところ悲惨な失敗でした。次の SQL クエリを LINQ に変換する必要があります。

SELECT 
MAX(A.NEXTPAYMENTDATE) as NextPaymentDate
, SUM(B.PurchasePrice) - SUM(A.Amount) AS BALANCE
, c.FirstName
, c.LastName
, b.[year]
, b.make
, b.model
FROM Payments A
JOIN Vehicles B ON A.VehicleId = B.Id
JOIN Customers C ON  b.CustomerId = c.Id
GROUP BY VehicleId, c.FirstName, c.LastName, b.[year], b.make, b.model
HAVING SUM(B.PurchasePrice) - SUM(A.Amount) > 0

これは私がこれまでに持っているものです。ある程度はうまくいきそうですが、ここから先の展開がわかりません。

var groupedpayments = 
    from payments in db.Payments
    group payments by new { payments.VehicleId }
    into paymentGroup
        let maxDate = paymentGroup.Max(x => x.NextPaymentDate)
        let paid = paymentGroup.Sum(x => x.Amount)
    select 
    new { Payments = paymentGroup.Where(x => x.NextPaymentDate == maxDate)};
4

1 に答える 1

2

それがあなたが必要としているものだと思います。

var query =
    Payments.Join(Vehicles, p => p.VehicleId, v => v.Id, (p, v) => new {p, v})
        .Join(Customers, d => d.v.CustomerId, c => c.Id, (d, c) => new {d, c})
        .GroupBy(r => 
                   new {
                           r.d.p.VehicleId,
                           r.d.v.year,
                           r.d.v.make,
                           r.d.v.model,
                           r.c.FirstName,
                           r.c.LastName
                       },
                 (g, data) =>
                   new {
                           FirstName = g.FirstName,
                           LastName = g.LastName,
                           Year = g.year,
                           Make = g.make,
                           Model = g.model,
                           NextPaymentDate = data.Max(dd => dd.d.p.NEXTPAYMENTDATE),
                           Balance = data.Sum(dd => dd.d.v.PurchasePrice)
                                      - data.Sum(dd => dd.d.p.Amount)})
         .Where(r => r.Balance > 0);
于 2012-12-08T19:29:16.087 に答える