使用Max()在实体框架中进行自联接

时间:2020-01-02 10:48:04

标签: c# entity-framework

我想从自引用表的父记录中获取记录,这些记录中必须包含多个子记录,所以我必须显示最大报价,我也想从另一个子表中显示子记录,我的查询也是这样

var query = (from pd in _context.Product_User_Transactions_Main
             join od in _context.Product_User_Transactions_Yarn on pd.TransactionID equals od.TransactionID
                     into t
             where (pd.UserPortalID == UserID && pd.TransactionTypeID == TransactionTypeID)
             select new
                     {
                         pd.TransactionID,
                         pd.ProductName,
                         pd.Quantity,
                         pd.Price,
                         pd.TransactionTypeID,
                         pd.UOMID,
                         pd.UOM.UOM1,
                         pd.RefTransactionID,
                         pd.PaymentTermID,
                         pd.UserPortalID,
                         pd.UserSessionID,
                         pd.TransactionTime,
                         Offer = (from m in _context.Product_User_Transactions_Main join el in _context.Product_User_Transactions_Main on m.TransactionID equals el.RefTransactionID where el.RefTransactionID != null select el.Price).Max(),

                         pd.GEN_PaymentTerm.PaymentTermName,

                         Product_User_Transactions_Yarn = t.Select(p => new { p.YarnBlendID, p.YarnColorID, p.YarnCount, p.YarnDesc, p.YarnPly, p.YarnSourceID, p.YarnTypeID, p.TransactionID }),
                      }).ToList(); 

此查询返回错误的结果,因为此查询向所有我要在价格前显示特定报价的记录显示Max的一条记录。我的错误在哪里?我不知道

0 个答案:

没有答案