c#linq - 获取最大日期时间

时间:2011-10-18 16:35:41

标签: c# linq

我有以下内容:

from vouchers in pi.PIVouchers
join invoices in pi.Invoices on vouchers.VoucherNumber equals invoices.VoucherNumber
join customers in pi.Customers on vouchers.PremierCustomerID equals customers.PremierCustomerID
join refundOption in pi.RefundOptions on vouchers.RefundOptionID equals refundOption.RefundOptionID
join store in pi.Stores on vouchers.PremierStoreID equals store.PremierStoreID
join groupcompany in pi.GroupCompanies on store.GroupCompanyID equals groupcompany.GroupCompanyID 
 where
(
    ((bdv.Length == 9 && vouchers.VoucherNumber.Substring(13, 9) == bdv.Replace("-", ""))
     || (bdv.Replace("-", "").Length > 9 && vouchers.VoucherNumber == bdv.Replace("-", ""))) ||
     (passportNo.Length > 0 && customers.CustomerPassportNumber.ToLower() == passportNo.ToLower()) ||
    (surname.Length > 0 && name.Length > 0
     && customers.CustomerLastName.ToLower() == surname.ToLower()
    && customers.CustomerFirstName.ToLower() == name.ToLower())
 )
    && !(from vrt in pi.VoidReprintTrackings
      where vrt.Voided == true
      select vrt.VoucherID).Contains(vouchers.VoucherID) //Valid Vouchers & Voided Vouchers required
      && (groupcompany.GroupCompanyName.ToLower().Contains("printemp")) //Only Printemps Vouchers required
      select new
         {
         vouchers.VoucherID,
         vouchers.VoucherNumber,
         vouchers.VoucherEligibleAmount,
         vouchers.VoucherRefundAmount,
         vouchers.VoucherCommission,
         refundOption.RefundOptionDescription,
         vouchers.AgentID,
         vouchers.GuideID,
         vouchers.AgentISOCountryCode,
         vouchers.VoucherDateTime,
         invoices.InvoiceDateTime,
         customers.CustomerPassportNumber,
          customers.CustomerFirstName,
         customers.CustomerLastName,
          vouchers.Antenne
          }; 

由于发票表上的连接,一张以上的发票针对凭证, 此查询返回许多行..但是我只想返回返回的发票中最早的一行..

所以基本上我想通过vouchernumber分组并选择最早的发票(invoicedatetime)

关于我如何做到这一点的任何建议?

亲切的问候, 菲奥娜

感谢那些看过这个的人......我找到了一个解决方案:

 var voucherData = from vouchers in pi.PIVouchers
                                  join invoices in pi.Invoices on vouchers.VoucherNumber equals invoices.VoucherNumber
                                  into g
                                  join customers in pi.Customers on vouchers.PremierCustomerID equals customers.PremierCustomerID
                                  join refundOption in pi.RefundOptions on vouchers.RefundOptionID equals refundOption.RefundOptionID
                                  join store in pi.Stores on vouchers.PremierStoreID equals store.PremierStoreID
                                  join groupcompany in pi.GroupCompanies on store.GroupCompanyID equals groupcompany.GroupCompanyID 
                                  where
                                         (
                                         ((bdv.Length == 9 && vouchers.VoucherNumber.Substring(13, 9) == bdv.Replace("-", ""))
                                         || (bdv.Replace("-", "").Length > 9 && vouchers.VoucherNumber == bdv.Replace("-", ""))) ||
                                           (passportNo.Length > 0 && customers.CustomerPassportNumber.ToLower() == passportNo.ToLower()) ||
                                           (surname.Length > 0 && name.Length > 0
                                            && customers.CustomerLastName.ToLower() == surname.ToLower()
                                            && customers.CustomerFirstName.ToLower() == name.ToLower())
                                           )
                                        && !(from vrt in pi.VoidReprintTrackings
                                             where vrt.Voided == true
                                             select vrt.VoucherID).Contains(vouchers.VoucherID) //Valid Vouchers & Voided Vouchers required
                                        && (groupcompany.GroupCompanyName.ToLower().Contains("printemp")) //Only Printemps Vouchers required
                                  select new
                                  {
                                      vouchers.VoucherID,
                                      vouchers.VoucherNumber,
                                      vouchers.VoucherEligibleAmount,
                                      vouchers.VoucherRefundAmount,
                                      vouchers.VoucherCommission,
                                      refundOption.RefundOptionDescription,
                                      vouchers.AgentID,
                                      vouchers.GuideID,
                                      vouchers.AgentISOCountryCode,
                                      vouchers.VoucherDateTime,
                                      InvoiceDateTime = g.Max(i => i.InvoiceDateTime),
                                      customers.CustomerPassportNumber,
                                      customers.CustomerFirstName,
                                      customers.CustomerLastName,
                                      vouchers.Antenne
                                  }; 

0 个答案:

没有答案