Linq orderby相同字段的不同值

时间:2017-05-31 07:43:20

标签: c# entity-framework linq linq-to-sql linq-to-entities

我有这个问题:

investorData = from investor in db.Investors
               join loanapp in db.LoanApplications on investor.FundID equals loanapp.FundID into loanAppData
               from lapp in loanAppData.DefaultIfEmpty()
               join fundreport in db.FundReportLoanDatas on lapp.LoanId equals fundreport.LoanId into fundReportData
               from freport in fundReportData.DefaultIfEmpty()
               where investor.FundID == fundID
               orderby lapp.LoanId descending
               select new InvestorPortfolioVM()
               {
                   InvestorId = investor.InvestorID,
                   CurrentLTV = freport.CurrentLTV == null ? 0.0 : freport.CurrentLTV,
                   LoanId = lapp.LoanId,
                   SegLTV = freport.SegLTV == string.Empty ? "" : freport.SegLTV,
                   BorrowerName = lapp.BorrowerName,
                   LoanStatusId = lapp.LoanStatusId
               };

我现在想要实现的是按一个字段订购商品,LoanStatusId从没有LoanStatusId为4或8的贷款开始。

知道如何实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

我认为这会做你想要的。

investorData = from investor in db.Investors
               join loanapp in db.LoanApplications on investor.FundID equals loanapp.FundID into loanAppData
               from lapp in loanAppData.DefaultIfEmpty()
               join fundreport in db.FundReportLoanDatas on lapp.LoanId equals fundreport.LoanId into fundReportData
               from freport in fundReportData.DefaultIfEmpty()
               where investor.FundID == fundID
               orderby (lapp.LoanId >= 4 &&  lapp.LoanId <= 8) ? 1 : 0
               select new InvestorPortfolioVM()
               {
                   InvestorId = investor.InvestorID,
                   CurrentLTV = freport.CurrentLTV == null ? 0.0 : freport.CurrentLTV,
                   LoanId = lapp.LoanId,
                   SegLTV = freport.SegLTV == string.Empty ? "" : freport.SegLTV,
                   BorrowerName = lapp.BorrowerName,
                   LoanStatusId = lapp.LoanStatusId
               };