LINQ:使用Max Revision Filter进行多次连接

时间:2017-06-20 19:19:52

标签: c# linq

所以我有这句话:

var transrevmax = db.TRANSACTs
    .Where(x => x.SITE == "1" && 
                x.DATE_IN >= dateinchoice && 
                x.DATE_OUT <= dateoutchoice)
    .GroupBy(x => x.TICKET_NO)
    .Select(x => x.OrderByDescending(y => y.REV_NO).FirstOrDefault())
    .ToList();

并返回我希望以下联接的完整事务列表,以便从...获取值

var exportrecovery = (from trans in transrevmax
                      join detail in db.DETAILs on new { TICKET_NO = trans.TICKET_NO, REV_NO = trans.REV_NO } equals new { TICKET_NO = detail.TICKET_NO, REV_NO = detail.REV_NO }
                      join job in db.JOBs on new { JOB_CODE = trans.JOB_CODE, CUST_CODE = trans.CUST_CODE } equals new { JOB_CODE = job.CODE, CUST_CODE = job.CUST_CODE }
                      join customer in db.CUSTOMERs on trans.CUST_CODE equals customer.CODE
                      join invoiced in db.INVOICEDs on trans.TICKET_NO equals invoiced.TICKET_NO
                      where trans.DATE_IN >= dateinchoice && trans.DATE_OUT <= dateoutchoice && trans.STATUS.ToString().Trim() != "V" && trans.STATUS.ToString().Trim() != "E"
                      select new { ADDRESS = customer.ADDRESS, CITY = customer.CITY, STATE = customer.STATE, ZIP = customer.ZIP, FREIGHT = detail.HAUL_CHGE + detail.FUEL_CHGE, JOB_NAME = job.NAME, HAUL_TAX = detail.HAUL_TAX, INVOICE_NO = invoiced.INVOICE_NO, CUST_NAME = customer.NAME, TAX_CODE = customer.TAX_CODE, MAT_CHGE = detail.MAT_CHGE, MAT_TAX = detail.MAT_TAX, MAT_CODE = detail.MAT_CODE, QTY = detail.QTY, PRICE = detail.PRICE, DATE_MOD = trans.DATE_OUT, REV_NO = trans.REV_NO, SITE = trans.SITE, TICKET_NO = trans.TICKET_NO, CUST_CODE = trans.CUST_CODE, JOB_CODE = trans.JOB_CODE }
                      ).ToList();

...如果我运行第一行,我将事务仅限于那些具有最大修订版本(我想要的),并且如果我运行连接(将“transrevmax”替换为表“db.TRANSACTs”我得到了正确的票号范围,但它包括所有修订版。我对如何使用连接表并根据其(最大)修订号仅获取唯一行感到困惑。当这两个结合使用时,我会收到零行。请指教。

1 个答案:

答案 0 :(得分:0)

删除ToList创建中的transrevmax