加入内部列表

时间:2016-10-05 17:30:33

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

我有这个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()
                        };

viewmodel:

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条语句行。

此查询是否正确?

2 个答案:

答案 0 :(得分:2)

这是使用导航属性

的方法
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()
                   };

答案 1 :(得分:0)

  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. 更好的选择,使用实体框架,正在使用 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(....)
                       };