加入内部列表

Join with inner list

我有这个 linq 查询:

var investorData = from investor in db.Investors
                        join investorLine in db.InvestorStatementLines
                            on investor.InvestorID equals investorLine.InvestorID
                        where investor.UserId == userId
                        select new InvestorViewModel()
                        {
                            InvestorId = investor.InvestorID,
                            InvestorName = investor.Name,
                            FundingDate = investor.FundingDate,
                            DueDate = investor.DueDate,
                            FundsCommitted = investor.FundsCommitted,
                            FundsInvested = investor.FundsInvested,
                            StatementLines =
                                db.InvestorStatementLines.Where(s => s.InvestorID == investor.InvestorID)
                                    .Select(t => new InvestorStatementLineVM
                                    {
                                        Balance = t.Balance,
                                        Credit = t.Credit,
                                        Debit = t.Debit,
                                        InvestorStatementLineDetails = t.Details,
                                        Date = t.Date
                                    }).ToList()
                        };

视图模型:

public class InvestorViewModel
{
    public int InvestorId { get; set; }
    public string InvestorName { get; set; }
    public DateTime FundingDate { get; set; }
    public DateTime? DueDate { get; set; }
    public Decimal? FundsCommitted { get; set; }
    public Decimal? FundsInvested { get; set; }
    public List<InvestorStatementLineVM>  StatementLines { get; set; }
}

发生的事情是,一旦我执行查询,我将获得 125 条记录,这是该投资者的 StatementLines 的数量。所以我得到了 125 条相同的记录,但我期望一个结果在内部列表中有 125 行语句。

这个查询是否正确?

  1. 使用 GroupJoin 而不是加入:(_join x in y on x.a equals y.a into z_)

    var investorData = from investor in db.Investors
                            join investorLine in db.InvestorStatementLines
                            on investor.InvestorID equals investorLine.InvestorID
                            into investorLine
                            where investor.UserId == userId
    
                            select new InvestorViewModel()
                            {
                                InvestorId = investor.InvestorID,
                                InvestorName = investor.Name,
                                FundingDate = investor.FundingDate,
                                DueDate = investor.DueDate,
                                FundsCommitted = investor.FundsCommitted,
                                FundsInvested = investor.FundsInvested,
                                StatementLines = investorLine
                                    .Select(t => new InvestorStatementLineVM
                                    {
                                        Balance = t.Balance,
                                        Credit = t.Credit,
                                        Debit = t.Debit,
                                        InvestorStatementLineDetails = t.Details,
                                        Date = t.Date
                                    }).ToList()
                            };
    

    此外,不要执行子查询,只需使用您刚刚执行的连接中的数据即可。

  2. 更好的选择,使用entity framework,使用navigation properties然后你不需要执行连接,但你只需要 InvestorStatementLines 作为 investor 的 属性。

    设置导航属性:

    public class InvestorViewModel
    {
        public int InvestorId { get; set; }
        public string InvestorName { get; set; }
        public DateTime FundingDate { get; set; }
        public DateTime? DueDate { get; set; }
        public Decimal? FundsCommitted { get; set; }
        public Decimal? FundsInvested { get; set; }
        public virtual ICollection<InvestorStatementLineVM>  StatementLines { get; set; }
    }
    

    并且查询将非常简单:

    var investorData = from investor in db.Investors
                       where investor.UserId == userId
                       select new InvestorViewModel()
                       {
                           InvestorId = investor.InvestorID,
                           ....
                           StatementLines = investor.InvestorStatementLines.Select(....)
                       };
    

这是使用导航属性的方法

var investorData = from investor in db.Investors
                   where investor.UserId == userId
                   select new InvestorViewModel()
                   {
                       InvestorId = investor.InvestorID,
                       InvestorName = investor.Name,
                       FundingDate = investor.FundingDate,
                       DueDate = investor.DueDate,
                       FundsCommitted = investor.FundsCommitted,
                       FundsInvested = investor.FundsInvested,
                       StatementLines = investor.InvestorStatementLines
                           .Select(t => new InvestorStatementLineVM
                           {
                               Balance = t.Balance,
                               Credit = t.Credit,
                               Debit = t.Debit,
                               InvestorStatementLineDetails = t.Details,
                               Date = t.Date
                           }).ToList()
                   };